Jump to content

not sure why my python 3 code isnt working

Go to solution Solved by CornOnJacob,

i fixed that but the error is in the while loop

-snip-

The way you have it now, i.e.

while menu_input != "i" or menu_input != "I" or menu_input != "p" or menu_input != "P" or menu_input != "q" or menu_input != "Q":

at least one of those conditions will fail, because menu_input can't equal "i" and "p" at the same time. While menu_input does not equal "i" or it does not equal "p", it will display "invalid menu choice." You made a Catch-22 conditional.

as in the title im not sure why this isnt selecting the menu_input properly, any help would be great

 

def instructions():    print("Instructions: It's golf on your console.")    print("For each shot, you may use a driver, iron, or a putter - each shot will vary in distance.")    print("The average distance each club can hit is: Driver = 100m, Iron = 30m, Putter = 10m")    main()def play():    print("play def)")def quit():    print("quit def)")def main():    print()    print("Golf!")    print("(I)nstructions")    print("(P)lay round")    print("(Q)uit")    menu_input = str(input(">>> "))    while menu_input != "i" or menu_input != "I" or menu_input != "p" or menu_input != "P" or menu_input != "q" or menu_input != "Q":        print("Invalid menu choice.")        menu_input = input(">>> ")    if menu_input == "i" or "I":        instructions()    if menu_input == "p" or "P":        play()    if menu_input == "q" or "Q":        quit()print("Let's play golf, CP1200 style!")print("Written by Dean Edwards, June 2014")main()
Link to comment
https://linustechtips.com/topic/172430-not-sure-why-my-python-3-code-isnt-working/
Share on other sites

Link to post
Share on other sites

I suggest you also post this on stack overflow.

Current rig: CPU: AMD FX-8120  Cooling: Corsair H100i  Mobo: ASRock 970 Extreme 3  RAM: 8GB 1333Mhz  GPU: MSI GTX 660Ti Power Edition  Case: Fractal Design Define R4  Storage: 2TB Seagate HDD + 128GB Crucial SSD  PSU: be quiet! 730W bronze

 

Link to post
Share on other sites

Are you getting errors or is it just not working right?

I'm not entirely sure, but if I had to guess, it would be that something like

 if menu_input == "i" or "I":        instructions()

should be

 if menu_input == "i" or menu_input == "I":        instructions()

.

 

Also, use the .lower() or .upper() methods of the string (in this case menu_input) so you don't need to check lower and uppercase every time, just one or the other:

while menu_input.lower() != "i" or menu_input != "p" or menu_input != "q":        print("Invalid menu choice.")

Following the logic from the answer to this post, your code could also be:

while !(menu_input.lower() in ("i", "p", "q"): //not guaranteed to work        print("Invalid menu choice.")

P.S. Your code tags seem to be broken, try editing your post.

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to post
Share on other sites

i fixed that but the error is in the while loop

-snip-

The way you have it now, i.e.

while menu_input != "i" or menu_input != "I" or menu_input != "p" or menu_input != "P" or menu_input != "q" or menu_input != "Q":

at least one of those conditions will fail, because menu_input can't equal "i" and "p" at the same time. While menu_input does not equal "i" or it does not equal "p", it will display "invalid menu choice." You made a Catch-22 conditional.

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to post
Share on other sites

ok i seem to have fixed it for now, untill i break it again :P

Good luck :)

 

In the future, quote posts or use @username to notify people in the thread who didn't follow/subscribe (I only knew you replied because I manually checked the thread).

Also, hit "Mark Solved/Answered" if you are all set.

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to post
Share on other sites

Good luck :)

 

In the future, quote posts or use @username to notify people in the thread who didn't follow/subscribe (I only knew you replied because I manually checked the thread).

Also, hit "Mark Solved/Answered" if you are all set.

my bad tirst time posting in forums thanks for telling me tho :)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×