Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code help
#1
Hi.
I am programing an RPG and I have run into a simple yet strange problem (At least to me)

When the user is asked to wake up or not, I used this code:
wake = False

while wake == False:
   if actionWake1 == "Wake" or actionWake1 == "wake" or actionWake1 == "WAKE":
       print ("Your eyes flutter as they slowly open to see a bright light.....")
       break
   else:
       print ("You cannot do that currently")
       wake = False
However, the else just repeats "You cannot do that currently" infinitly....
A little help please?
Reply
#2
There are two possibilities. (1) actionWake1 was a correct value, and you hit the break right away or (2), it wasn't and the else-branch doesn't ever change actionWake1 at all and only sets wake to the the value that lets the loop continue.

If you walk through the code, you should see that there's no path for the loop to iterate other than once or forever.
Reply
#3
(Feb-21-2017, 09:02 PM)micseydel Wrote: There are two possibilities. (1) actionWake1 was a correct value, and you hit the break right away or (2), it wasn't and the else-branch doesn't ever change actionWake1 at all and only sets wake to the the value that lets the loop continue.

If you walk through the code, you should see that there's no path for the loop to iterate other than once or forever.

So how would I make it so the else only repeats once?
Reply
#4
In the else-branch you probably want to re-prompt the user for actionWake1. Whatever you did before the loop, you should do that again.
Reply
#5
Why you check for three values when you can do it once.

if actionWake1.lower() == "wake":
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Feb-21-2017, 10:13 PM)wavic Wrote: Why you check for three values when you can do it once.

if actionWake1.lower() == "wake":

Thanks!
I am 15 and I have only been coding on Python for a year and this is my first actual project on it so I am bound to make a few errors and do things inefficiently
Reply
#7
Errors are something good. Keep coding Whistle
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Feb-21-2017, 09:17 PM)micseydel Wrote: In the else-branch you probably want to re-prompt the user for actionWake1. Whatever you did before the loop, you should do that again.

I will try this now..

(Feb-21-2017, 09:17 PM)micseydel Wrote: In the else-branch you probably want to re-prompt the user for actionWake1. Whatever you did before the loop, you should do that again.

OH MY GOD

THANKS YOU SO MUCH!

THAT WAS DRIVING ME CRAZY!!

Big Grin Big Grin Big Grin
Reply


Forum Jump:

User Panel Messages

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