2015-12-08 60 views
-1
import random 


print("Hey there, player! Welcome to Emily's number-guessing game! ") 
name=input("What's your name, player? ") 

random_integer=random.randint(1,25) 

tries=0 
tries_remaining=10 

while tries < 10: 
    guess = input("Try to guess what random integer I'm thinking of, {}! ".format(name)) 
    tries += 1 
    tries_remaining -= 1 

# The next two small blocks of code are the problem. 

    try: 
    guess_num = int(guess) 
    except: 
    print("That's not a whole number! ") 
    tries-=1 
    tries_remaining+=1 


    if not guess_num > 0 or not guess_num < 26: 
    print("Sorry, try again! That is not an integer between 1 and 25! ") 
    break 



    elif guess_num == random_integer: 
    print("Nice job, you guessed the right number in {} tries! ".format(tries)) 
    break 


    elif guess_num < random_integer: 
    if tries_remaining > 0: 
     print("Sorry, try again! The integer you chose is a litte too low! You have {} tries remaining. ".format(int(tries_remaining))) 
     continue 
    else: 
     print("Sorry, but the integer I was thinking of was {}! ".format(random_integer)) 
     print("Oh no, looks like you've run out of tries! ") 



    elif guess_num > random_integer: 
    if tries_remaining > 0: 
     print("Sorry, try again! The integer you chose is a little too high. You have {} tries remaining. ".format(int(tries_remaining))) 
     continue 
    else: 
     print("Sorry, but the integer I was thinking of was {}! ".format(random_integer)) 
     print("Oh no, looks like you've run out of tries! ") 

我会尽量解释这一点以及我可以...我试图让问题区域允许输入猜测后再次用户输入除1和25之间的整数以外的任何东西,但我无法弄清楚如何去做。我该如何做到这一点,以便用户可以选择在他们赢得或失败后重新启动程序?不知道我在做什么错,Python猜数游戏

编辑:请不要在问题中没有别的声明,因为没有相反的输出。

回答

0

这将解决错误的间隔

if not guess_num > 0 or not guess_num < 26: 
    print("Sorry, try again! That is not an integer between 1 and 25! ") 
    continue 

对于剩下的,你可以做这样的事情

创建一个方法,并坚持在你的游戏数据

def game(): 
    ... 
    return True if the user wants to play again (you have to ask him) 
    return False otherwise 

play = True 
while play: 
    play = game() 
+0

谢谢哈哈,我甚至没有意识到我忘记了那个 – Nightingale

1

使用函数将所有内容放入函数中,如果用户想再次尝试,则再次调用该函数! 这将重新开始整个过程​​!如果用户想重新启动,也可以完成此操作。 再次调用该方法是一个很好的计划。在方法/函数中放入完整的东西。