在Jython环境中为学生编写猜谜游戏(JES)

时间:2016-09-24 12:09:10

标签: if-statement computer-science jes

我正在尝试使用下面的伪代码在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 " )

1 个答案:

答案 0 :(得分:0)

由于用户无法猜出randnum所产生的相同数量,因此它停留在循环中 “虽然userguess!= a”它永远不会是真的