Python 为游戏循环提问

Python 为游戏循环提问,python,Python,我开始编写一个游戏只是为了练习。但是,用户需要输入奇数才能播放。如果他们没有,那么我希望程序要求他们输入一个奇数,然后他们循环播放。我将此代码放在else语句中,但如果输入奇数,它将不会再次循环 问题2: 如何让程序在循环运行时显示游戏1、游戏2等,无论“游戏”中的输入是多少次 有人能帮忙吗 games = input("How many games would you like to play?") for i in range(games): if games % 2 == 1:

我开始编写一个游戏只是为了练习。但是,用户需要输入奇数才能播放。如果他们没有,那么我希望程序要求他们输入一个奇数,然后他们循环播放。我将此代码放在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')
试试这个:

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: '))

在我看来,你的困惑在于几个铸造错误。请注意,输入返回一个类型字符串,您试图将其用作整数。请尝试以下代码:

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")

for循环在这里是错误的工具。你需要在适当的条件下使用while循环。下一票不是我的,但我也很难理解你的问题。。。你想重复要求奇数游戏次数吗?在我看来,在这里使用while循环会更好。一旦用户输入一个奇数,你就可以打断它。可能是重复的你问了两个不同的问题,这使得你的问题对于SO标准来说过于宽泛。对于第一个,请确切了解我要查找的内容。我需要更好地练习。这就是我错过的。我不知道你能把整个条件变为真,然后在它下面写一个for循环。