![]() |
My if and while loop statements aren't working - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: My if and while loop statements aren't working (/thread-5667.html) |
My if and while loop statements aren't working - RedSkeleton007 - Oct-15-2017 I just get a syntax error with the if statement: #adding two integers together when one of them is a sring by default: x = 15 y = "5" z = x + int(y) print(z) if x > str(y) print("x is greater.")As for my while loop, it isn't terminating like I want it to when the user enters 17: #number guessing game that increments the number of guesses: guess = 0 correctNum = 17 while guess != correctNum: guess = input("Guess a number between 0 and 30: ") continue #return to beginning of while loop print("You guessed it! Number " + correctNum + " is the magic number!") print("Thanks for playing.") RE: My if and while loop statements aren't working - wavic - Oct-15-2017 The while loop doesn't terminate because the condition is against an integer but everything user inputs are string. So the condition is always True. RE: My if and while loop statements aren't working - RedSkeleton007 - Oct-16-2017 Am I really stuck doing it backwards if I use Python? It seems so much more straight forward to declare both the variables and their data types ahead of time as I do in the commented out lines 2 and 3 of the following code: #number guessing game that increments the number of guesses: #int(guess) = 0 #int(correctNum) = 17 #while guess != correctNum guess = 0 correctNum = 17 while int(guess) != int(correctNum): guess = int(input("Guess a number between 0 and 30: ")) continue #return to beginning of while loop print("You guessed it! Number " + str(correctNum) + " is the magic number!") print("Thanks for playing.") RE: My if and while loop statements aren't working - sparkz_alot - Oct-16-2017 Not sure what you mean by doing it backwards. You've already declared 'guess' and 'correctNum' as numeric (you don't need to re-declare them in the while statement) and revised your input() to look for a number, then you are done. guess = 0 correctNum = 17 while guess != correctNum: guess = int(input("Guess a number between 0 and 30: ")) continue #return to beginning of while loop print("You guessed it! Number {} is the magic number!".format(correctNum)) print("Thanks for playing.")
RE: My if and while loop statements aren't working - RedSkeleton007 - Oct-21-2017 Alright, now I'm trying to make some elif statements work, but for some reason, I'm stuck in an infinite while loop. Here's the code: counter = 0 heard = "" while counter < 5: print("Go play some Donkey Kong Country 2 for Super Nintendo!") heard = input("Did you hear what I said?") if heard != "n" or heard != "y": print("Invalid entry. Enter y for yes, or n for no.") continue elif heard == "n" and counter < 5: counter += 1 continue elif heard == "y" and counter < 5: print("Alright! Don't forget to collect all the Kremcoins too.") break elif heard == "n" and counter == 5:#5 no end statement print("Say what one more time! I dare you MF! Say what again!") break # is this break really necessary, since the counter is now 5?However, the output is this: ======= RESTART: I:\Python\Python36-32\SamsPrograms\WhileLoopBasics.py ======= Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?k Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?k Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?k Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?k Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?k Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?n Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?n Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?n Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?n Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?n Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?n Invalid entry. Enter y for yes, or n for no. Go play some Donkey Kong Country 2 for Super Nintendo! Did you hear what I said?As you can see, the while loop continues after I entered the k four times because k is not a valid entry, but why doesn't it jump down to: elif heard == "n" and counter == 5:#5 no end statementI've entered n more than five times, haven't I? What's going on? RE: My if and while loop statements aren't working - buran - Oct-21-2017 so first of all, your if statement ob line 6 will always be True, because you use or , it should be and second - if count==5 it will never enter the loop body, because the loop condition is while counter < 5 , thus elif condition on line 15 does not make sense, i.e. it will never be reached.
RE: My if and while loop statements aren't working - RedSkeleton007 - Oct-21-2017 (Oct-21-2017, 06:26 AM)buran Wrote: second - if count==5 it will never enter the loop body, because the loop condition is Why can't the program just print out line 16 in the following code before exiting the while loop when the counter is incremented to 5? counter = 0 heard = "" while counter < 5: print("Go play some Donkey Kong Country 2 for Super Nintendo!") heard = input("Did you hear what I said?") if heard != "n" and heard != "y": print("Invalid entry. Enter y for yes, or n for no.") continue elif heard == "n" and counter < 5: counter += 1 continue elif heard == "y" and counter < 5: print("Alright! Don't forget to collect all the Kremcoins too.") break else: print("Say what one more time! I dare you MF! Say what again!") break # is this break really necessary, since the counter is now 5? RE: My if and while loop statements aren't working - buran - Oct-21-2017 because once counter is incremented to 5 in some of the previous iterations, it will not enter the loop at all. that is also the reason while you don't need to check counter<5 in lines 9 and 12. counter = 0 while counter<5: print(counter) counter += 1 eventually you can change it to while<=5 counter = 0 while counter<=5: print(counter) counter += 1
|