【发布时间】:2016-09-24 23:26:19
【问题描述】:
我正在尝试使用下面的伪代码在 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 " )
【问题讨论】:
标签: if-statement computer-science jes