使用elif控制流和语法错误



我正在尝试创建一个执行以下内容的程序:

  1. secret_num==生成1-10之间的随机#
  2. 让用户在1-10之间猜测这个数字
  3. 如果猜测==secret_num祝贺他们,并提供猜测次数number_guesses(减去任何重复的猜测)
  4. 解释重复的猜测
  5. 让他们知道猜测是过高还是过低

我知道这对你们很多人来说都是abc的东西,但我在语法、缩进和控制流方面遇到了典型的新程序员问题。现在我正在获取elif guess==secret num.否则我就无法使用此代码来解释重复项以挽救我的生命。如果有任何帮助,我将不胜感激。非常感谢。

import random
def game():
# generate a random number between 1 -100 and initiate repeat guess list
    secret_num=random.randint(1,10)
# initiate duplicate list and number of guesses
    already_guesses = []
    number_guesses = 0
    while True:
# ask for user guess
        guess = int(input('Guess a number from 1-10> '))
# help with guesses and announce if it is correct number!
# catch someone if they submit the same number
        if guess in already_guesses:
            print ("You already guessed that number, try again")
        already_guesses.append(guess)
        number_guesses += 1
        elif guess == secret_num:
            print("whooohoo! you guessed it, my number is {}".format(secret_num))
            print("It took you {} guesses".format(number_guesses - len([already_guesses])))
            break
        elif guess < secret_num:
            print("You're getting hot, try a higher number")
        else:
            guess > secret_num
            print("You're cold, try a lower number")
# ask if they want to play again
    else:
        play_again = input("Do you wanna play again? y/n")
        if play_again != 'n':
            game()
        else:
            print("Bah Bye!")
game()

您的代码还有一些其他问题(在注释中提到的缩进之后):

import random
def game():
    # Generate a random number between 1-100 and initiate repeat guess list
    secret_num=random.randint(1,10)
    # Initiate duplicate list
    # Consider using a set instead so you don't have to cast it into a set later
    already_guesses = []
    while True:
    # Ask for user guess
        guess = int(input('Guess a number from 1-10> '))
        # Help with guesses and announce if it is correct number!
        # Catch someone if they submit the same number
        if guess in already_guesses:
            print ("You already guessed that number, try again")
        elif guess == secret_num:
            # Format your strings properly
            print("whooohoo! you guessed it, my number is {0}".format(secret_num))
            # See how many unique guesses there were and add the correct guess to it (+1)
            # To do so just use a set
            print("It took you {0} guesses".format(
                len(set(already_guesses))+1
            ))
            break
        elif guess < secret_num:
            print("You're getting hot, try a higher number")
        else:
            guess > secret_num
            print("You're cold, try a lower number")
        # Make sure to append the guess to the already_guessed list in any case, not just if the number is in already_guesses
        # If you only do that in your original if block, already_guesses will always remain empty
        already_guesses.append(guess)
    # Ask if they want to play again
    play_again = input("Do you wanna play again? y/n")
    if play_again != 'n':
        game()
    else:
        print("Bah Bye!")
game()

相关内容

  • 没有找到相关文章

最新更新