Python Forum
Why is this code not working?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is this code not working?
#1
I have been sitting in front of my PC for 5 hours straight now trying to solve this, but it just comes to the question and if i answer yes it breaks the loop as wanted, but if I type in no it crashes. Any help is appreciated, thank you, -Ivan
def clss():
    import os
    os.system('cls' if os.name == 'nt' else 'clear')
def pause():
    raw_input("Press Enter to continue")
print ("No Life Ent.")
pause()
clss()
print ("The No Life inc. Quiz")
pause()
clss()
print ("Begin game?")
a1 = raw_input("Type in yes or no: ")
while a1 !="yes":
    if a1 == yes:
        print ("Ok, let us begin then")
        break
    else:
        print("Type in yes.")
        continue
pause()
print("THANK THE LORD THIS WORKS!")
pause()
I've got Python 2.7.13 if that is of any help.
Reply
#2
Hello!
Perhaps there are cases when importing a module inside a function is reasonable but I think this in not one.

Well, if you answer 'no', the loop will never stop. You don't change the value of a1 anywhere inside the loop so its condition will return True every time.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
In the future, please elaborate on your promlem.
If I tell you my car is not working, can you fix it?
Reply
#4
You can set while True and a1 == 'yes'(string) else will be 'no',and break out on both.
But you soon get into trouble with this design.
Keeping all in functions make it easier to to jump around like playing a game and back to menu.
Eg:
from six.moves import input
import time

def my_game():
   print('Game running')
   for i in reversed(range(5)):
       time.sleep(1)
       print('Playing {}'.format(i))
   input('Push enter to retun to menu')

def menu():
   while True:
       print('(1) Play game')
       print('(Q) Quit')
       choice = input('Enter your choice: ').lower()
       if choice == '1':
           my_game()
       elif choice == 'q':
           return False
       else:
           print('Not a correct choice: {}'.format(choice))

if __name__ == '__main__':
   menu()
So here always fall back into menu,from here can Quit game or play new game.
You can build on game/menu function over jump over to other function,
can always fall back to menu where there is a way out(Quit).

With function you can build quite complex structure and your code don't get messy.
Using class is and other way to structure code,not needed for small games for larger games it can be useful Undecided
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 908 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 834 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 970 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,030 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,862 Mar-21-2022, 10:12 AM
Last Post: End3r
  I don't undestand why my code isn't working. RuyCab 2 1,956 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 2,848 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Short code for EventGhost not working Patricia 8 3,584 Feb-09-2021, 07:49 PM
Last Post: Patricia
  Code no longer working yk303 14 9,947 Dec-21-2020, 10:58 PM
Last Post: bowlofred
  autocomplete working code sample not working... aviper4u 0 1,602 Oct-24-2020, 03:04 AM
Last Post: aviper4u

Forum Jump:

User Panel Messages

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