Python Forum
My if and while loop statements aren't working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My if and while loop statements aren't working
#1
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.")
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.")
Reply
#4
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.")
Output:
Guess a number between 0 and 30: 5 Guess a number between 0 and 30: 17 You guessed it! Number 17 is the magic number! Thanks for playing. Process finished with exit code 0
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
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 statement
I've entered n more than five times, haven't I? What's going on?
Reply
#6
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.
Reply
#7
(Oct-21-2017, 06:26 AM)buran Wrote: 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.

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?
Reply
#8
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
Output:
0 1 2 3 4
eventually you can change it to while<=5

counter = 0
while counter<=5:
    print(counter)
    counter += 1
Output:
0 1 2 3 4 5
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  while loop not working-I am using sublime text editor mma_python 4 1,126 Feb-05-2023, 06:26 PM
Last Post: deanhystad
  Multiple Loop Statements in a Variable Dexty 1 1,204 May-23-2022, 08:53 AM
Last Post: bowlofred
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,484 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Too many if statements working as "tags" zeek 3 1,988 Sep-20-2021, 05:22 PM
Last Post: deanhystad
  Parameters aren't seen inside function Sancho_Pansa 8 2,908 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa
  Why is the while loop not working? mcoliver88 5 3,099 Aug-18-2020, 03:27 PM
Last Post: deanhystad
  Infinite loop not working pmp2 2 1,649 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  Loop not working Nonameface 8 2,908 Jul-19-2020, 12:27 PM
Last Post: snippsat
  Unable to combine print statements in for loop adeana 2 1,994 Jun-12-2020, 05:08 PM
Last Post: adeana
  for loop script over telnet in Python 3.5 is not working abhijithd123 1 2,897 May-10-2020, 03:22 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020