我正在尝试使用下面的伪代码在JES中编写一个猜谜游戏:
生成一个随机的个位数
让用户猜数字
如果用户没有猜对,给他们一个提示-指出这个数字是偶数还是奇数,并要求用户再猜一次。
如果用户没有猜对,给另一个提示-指出这个数字是否是3的倍数,并要求用户再猜一次。
如果用户没有猜对,给出另一条线索——指出数字是小于还是大于5,并要求用户再猜一次。
如果用户没有猜对,
显示一个方框,指示正确答案和用户猜测的次数。
如果用户猜对了,显示一个方框,表明他们猜对了,以及需要猜多少次
显示用户玩猜谜游戏的时间。
有这么多的条件,我不知道如何让程序问每个问题,分析答案,如果条件不满足,就继续下一个问题。当我运行这段代码时,它只是被困在要求用户猜测的循环中,而不是通过if语句给用户提示,如果它们是不正确的。
下面是我当前的代码,显然是不正确的。我也意识到这篇文章中可能存在缩进问题。
from random import *
from time import *
def main():
a= randrange( 0, 11 )
start= clock()
numberOfGuesses= 0
userGuess= requestInteger( " Please guess a single digit number " )
while userGuess != a:
userGuess= requestInteger( " Please guess a single digit number " )
if userGuess % 2 == 0:
showInformation( " Incorrect! Please try again. Hint: The number is even " )
if userGuess % 2 != 0:
showInformation( " Incorrect! Please try again. Hint: The number is odd " )
if userGuess % 3 != 0:
showInformation( " Incorrect! Please try again. Hint: The number is not a multiple of 3 " )
if userGuess > 5:
showInformation( " Incorrect! Please try again. Hint: The number is greater than 5 " )
if userGuess < 5:
showInformation( " Incorrect! Please try again. Hint: The number is greater than 5 " )
else:
showInformation( " Correct " )
修订后的工作规则
from random import*
from time import*
def main ():
a= randrange( 0, 10 )
start= clock()
numberOfGuesses= 0
userGuess= requestNumber( " Please guess a single digit number " )
end= clock()
elapsedTime= end - start
if userGuess != a and userGuess % 2 == 0:
showInformation( " Incorrect! Please try again. Hint: The number is even " )
userGuess= requestNumber( " Please guess a single digit number " )
numberOfGuesses= numberOfGuesses + 1
elif userGuess != a and a % 2 != 0:
showInformation( " Incorrect! Please try again. Hint: The number is odd " )
userGuess= requestNumber( " Please guess a single digit number " )
numberOfGuesses= numberOfGuesses + 1
if userGuess != a and a % 3 != 0:
showInformation( " Incorrect! Please try again. Hint: The number IS NOT a multiple of 3 " )
userGuess= requestNumber( " Please guess a single digit number " )
numberOfGuesses= numberOfGuesses + 1
if userGuess != a and a > 5:
showInformation( " Incorrect! Please try again. Hint: The number is greater than 5 " )
userGuess= requestNumber( " Please guess a single digit number " )
numberOfGuesses= numberOfGuesses + 1
if userGuess != a and a < 5:
showInformation( " Incorrect! Please try again. Hint: The number is less than 5 " )
userGuess= requestNumber( " Please guess a single digit number " )
numberOfGuesses= numberOfGuesses + 1
if userGuess == a:
showInformation( " Correct! " )
showInformation( " The correct answer was" + " " + str(a) + " It took you " + str(elapsedTime) + " " + " seconds to complete this game " )
elif userGuess != a :
showInformation( " Incorrect! " + " " + " The correct answer was" + " " + str(a) + " It took you " + str(elapsedTime) + " " + " seconds to complete this game " )
它被困在循环中,因为用户无法猜测与randnum生成的相同的数字"while userguess !=a "它永远不会为真