Python Forum
what is wrong with this code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is wrong with this code?
#1
def check1():
	import time
	if stat1 < 5:
		while stat1 <5:
			print('smaller_than_5')
			time.sleep(1)
        print('same_or_bigger_than_5')     #I get the error message here.
	else:
		print('same_or_bigger_than_5')
I'm trying to code something that can check a number called 'stat1' every second and see if it is bigger or smaller than 5. I want it to keep showing "smaller than 5" until it same or bigger than 5 and finally stop by showing the "same or higher than 5" message. But I keep getting an error message for line 7. But if the line is removed, it just stops when it is 5 or bigger than 5 and doesn't show the message.
Reply
#2
You'd need to show the full error message. But the code you show shouldn't have an error there.

But I don't see how line 7 would be reached. If the function is entered with stat1 < 5, then it will run the while loop forever with nothing to change stat1. Are you leaving out any lines that would modify the variable?
Reply
#3
What error message do you get ?

If you remove the line 7, it makes sense that you don't see the message.
That would be triggered by the else statement, but you are still in the if... part.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 480 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 850 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,555 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,250 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,781 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,539 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,526 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,630 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,498 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 5,020 Sep-16-2020, 02:00 PM
Last Post: MrLeads

Forum Jump:

User Panel Messages

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