增量和减量不起作用

时间:2018-06-09 04:42:41

标签: python python-3.x

嗨,我正在制作一个Python猜测游戏程序而且我无法减少价值。这是我的计划:

import random as rand

print('\t\t\tGuess Game\n')
print('Enter a Number Between 1 and 20')

while True:

    a = rand.randint(1, 20)

    for i in range(1, 5):
        guess = 0

        guess = int(input('Enter a Number : '))

        if guess == a:
            print('You Are Right\n')
            break

        if guess == 0 or guess < 0:
            print('Please Enter a Positive Integer\n')
            i -= 1
            continue

        if guess > 20:
            print('Please Enter a Reasonable Number\n')
            i -= 1
            continue

        if guess > a:
            if guess <= (a + 5):
                print('Your Number is Little High\n')

            else:
                print('Your Number is Too High\n')

        if guess < a:
            if guess >= (a - 5):
                print('Your Number is Little Low\n')

            else:
                print('Your Number is Too Low\n')

    if guess != a:
        print('My Number Was %s. Please try The Program Again If You Like\n' % a)

    play = str(input('Do You Want to play Again? '))
    play.lower()
    if play.startswith('y'):
        continue

    elif play.startswith('n'):
        break

    else:
        while play is not ((play.startswith('y') and play.endswith('s')) or play == 'y') or ((play.startswith('n') and play.endswith('o')) or play == 'n'):
            play = input('Please Enter a Yes or No')

我 - = 1不工作。

感谢。

编辑 这是7次猜测,而使用负整数

将随机导入为rand

i = 5#猜测次数 a = rand.randint(1,20)

而我&gt; 0:     guess = int(input())

if guess == a:
    print("You won!")
    break

if guess == 0:
    i = i + 1
    continue

i = i - 1

1 个答案:

答案 0 :(得分:0)

使用for循环,每次迭代都会设置i。因此,如果从i = 5 # Number of guesses while i > 0: # Do your game loop here. Subtract from i to lose a guess. if guess == a: print("You won!") break 变量中减去一个变量,它将被简单地设置回原来的变量(从5变为1)。让你的循环运行直到玩家用尽猜测:

i > 0

如果玩家猜对了,“你赢了!”将被打印,循环将结束。如果玩家已经没有猜测,那么循环也将结束 - 但不打印消息。

如果您想检查玩家是否赢了,请执行var rangeSlider = function() { var slider = $(".range-slider"), range = $(".range-slider__range"), value = $(".range-slider__value"); slider.each(function() { value.each(function() { var value = $(this) .prev() .attr("value"); $(this).html("All"); }); range.on("input", function() { var str = ""; if(this.value == 1) str = "All"; else if(this.value == 2) str = "Group"; else str = "Me"; $(this) .next(value) .html(str); }); }); }; rangeSlider();。如果玩家获胜,这将是真实的,因为仍然会有一些猜测。