本文介绍了While循环猜测数字游戏-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试进行猜测1-10之间的数字"游戏,但是while循环似乎一直在运行.我想编程让用户猜一个数字,然后显示它的值是否太高或太低等,然后自动重新开始(循环)以允许用户再次选择.此代码使它永远运行. 你们可以帮我吗?
I'm trying to make a 'guess the number between 1-10' game but the while loops seems to keep running. I want to program to let the user guess a number then display if its too high or low etc then start again automatically (loop) to allow the user to pick again. This code makes it run forever though. Can you guys help me?
import random def numberGuess(): printNow("I'm thinking of a number between 1 and 10") guess = 0 # give guess a starting value randNum = random.randrange(1,11) # this line generates a random number guess = int(input("Try to guess the number:")) # ask user for a number print randNum while guess != randNum: if (guess == randNum): print "You got it!" if (guess > randNum): print "Wrong! You guessed too high" if (guess < randNum): print "Wrong! You guessed too low"推荐答案
您忘了在循环内猜测
while guess != randNum: guess = int(input("Try to guess the number:")) if (guess > randNum): print "Wrong! You guessed too high" if (guess < randNum): print "Wrong! You guessed too low" print "You got it!"更多推荐
While循环猜测数字游戏
发布评论