循环游戏问题

时间:2019-03-05 13:32:06

标签: python

我开始编写游戏代码以进行练习。但是,用户需要输入一个奇数才能播放。如果没有,我希望程序向他们询问一个奇数,它们会循环播放并再次播放。我将此代码放在else语句中,但如果输入奇数,它将不会再次循环。

问题2: 我如何让程序在循环运行时显示“游戏1”,“游戏2”等,而无论“游戏”中的输入是多少次?

有人可以帮忙吗?

games = input("How many games would you like to play?")

for i in range(games):
  if games % 2 == 1:
     print('Game 1')
     # code here
  else:
     input('Enter an odd number')

2 个答案:

答案 0 :(得分:1)

尝试一下:

games = int(input("How many games would you like to play?"))

while True:        
    if games % 2 == 1:        
        for i in range(games):          
            print('Game', i+1 )
        break

    else:
        games = int(input('Enter an odd number: '))

答案 1 :(得分:0)

在我看来,您的困惑在于几个转换错误。请注意,input返回类型string,您尝试将其用作integer。请尝试以下代码:

games = input("How many games would you like to play? ")
numberOfGames = int(games)

for i in range(numberOfGames):
  print('Processing Game ' + str(i))
  testVal = input('Enter an odd number ')
  if int(testVal) % 2 == 1:
      print("Congratts! " + testVal + " is odd!\n\n")
  else:
      print("You Loose. " + testVal + " is even.\n\n")