-1

I am starting to code a game just to practice. However, the user needs to enter an odd number to play. If they don't then I want the program to ask them for an odd number they loop and play again. I put this code in the else statement but if I enter an odd number it will not loop again.

Question 2: how can I get the program to display Game 1, Game 2, etc as the loop runs however many times the input in 'Games' is?

Can someone help?

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')
skaul05
  • 2,154
  • 3
  • 16
  • 26
ajburnett344
  • 79
  • 1
  • 9
  • A `for` loop is the wrong tool here. You need to use a `while` loop with the appropriate conditional. – Christian Dean Mar 05 '19 at 13:39
  • The downvote isn't mine, but I have problems understanding your question, too... Do you like to repeat asking for an odd number `games`-times? It seems to me that a `while` loop would be much better here. Then you could break it once the user inputs an odd number. – nostradamus Mar 05 '19 at 13:41
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Mar 05 '19 at 13:42
  • You asked two different questions, which makes your question too broad for SO standards. For the first one, see https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Thierry Lathuille Mar 05 '19 at 13:43

2 Answers2

1

Try this:

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: '))
Heyran.rs
  • 501
  • 1
  • 10
  • 20
  • exactly what i was looking for. I need to practice while loops better. That was what i was missing. I did not know you can make the whole condition True then write a for loop under it. – ajburnett344 Mar 05 '19 at 14:02
0

It appears to me that you your confusion lies in a couple casting errors. Please note that input returns a type string, which you attempt to use as an integer. Try the following code instead:

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")
armitus
  • 712
  • 5
  • 20