Python Forum
if statement always testing as true?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if statement always testing as true?
#1
Hello, I've been tasked with creating a simple choice based text game revolving around a general theme, which I choose to be Greek mythology. For the life of me I cannot figure out why the if statement on line 11 always runs regardless of the previous input. I'm very new to this and any insight would be much appreciated! Here is my code:

def start():
	print("Greetings Traveler! I am the mystical spirit that inhabits these lands")
	
	name = input("What is your name traveler?...  ")
	print(f"Oh, so your name is {name}?")
	print("Nice to meet you.")
	
	print(f"Might I ask which god you follow in this war {name}?")
	god = input("Zeus, Poseidon, or Hades?.. ")
	
	if god == "Zeus" or "zeus":      #Always get stuck here regardless of the above input. hmmmmm....
		#zeus_prayer()
		print("Zeus chosen")
	elif god == "Poseidon" or "poseidon":
		#poseidon_prayer()
		print("Poseidon chosen")
	elif god == "Hades" or "hades":
		#hades_prayer()
		print("Hades chosen")
	elif god == "No one" or "No god" or "Nobody":
		print(f"So you are godless, {name}?")
		print("I like your style.")
		print(f"I shall now call you {name} the godless.")
		name = name + "the godless"
		
		print(name)
		#the_vision()
	else:
		print("Oh? I haven't heard of that god. How boring, perhaps you should worship someone who is worthy.")
		print("Tell you what, I'm going to pretend like I didn't hear that and we're going to start this conversation over. Deal?")
		print("Hopefully you could try to be more entertaining next time.")
		
		start()

start()
Here is the output:

Output:
Greetings Traveler! I am the mystical spirit that inhabits these lands What is your name traveler?... Tyler Oh, so your name is Tyler? Nice to meet you. Might I ask which god you follow in this war Tyler? Zeus, Poseidon, or Hades?.. Hades Zeus chosen
Regardless of what input I put in for line 9, the if statement on line 11 always runs and I can't seem to figure out why.
Reply
#2
This is a very common Python pitfall, please take a look at https://python-forum.io/Thread-Multiple-...or-keyword
Reply
#3
I'm also starting Python, already 2 weeks. So I'll try to help you out a little bit with your code.

print(f" ") I don't know the meaning of "f" before quotes. Besides that "f" your prints before else run well.

Issues_list:
  • From (if to last elif)your booleans are half right and half wrong. XD

I had the same mistake before, but after a good research I was still confused. So I decided to try adding the variable after the boolean.

If god == "King Behemoth" or "king behemoth": I could't make it run properly with a loop.
print("He is the mighty one!")


if god == "King Behemoth" or god == "king behemoth":
print("He is the mighty one!")

  • After (else:) all your print 29 to 31 lanes have issues with your contractions words, such as:

haven't
didn't
etc...

All contractions in python cannot run if you print them directly like: print("I can't understand"). In Python you need to use a backslash \ between the contraction and the apostrophe. print("I can\'t understand")

note: backslash (alt + 92) (\)
  • line 30 too long.



Sorry if I'm not good at explaining things because I'm still new with coding, but I'll try my best next time. Also i don't know if this is the result you wanted.


def start_choice():

    print("Greetings Traveler! I am the mystical spirit that inhabits these lands")

    name = input("What is your name traveler?...  ")

    print(f"Oh, so your name is {name}?")
    print("Nice to meet you.")

    print(f"Might I ask which god you follow in this war {name}?")
    god = input("Zeus, Poseidon, or Hades?.. ")

    if god == "Zeus" or god == "zeus":  # Always get stuck here regardless of the above input. hmmmmm....
        # zeus_prayer()
        print("Zeus chosen")

        print("Welcome!")

    elif god == "Poseidon" or god == "poseidon":
        # poseidon_prayer()
        print("Poseidon chosen")

        print("Welcome!")

    elif god == "Hades" or god == "hades":
        # hades_prayer()
        print("Hades chosen")

        print("Welcome!")

    elif god == "No one" or god == "No god" or god == "Nobody":
        print(f"So you are godless, {name}?")
        print("I like your style.")
        print(f"I shall now call you {name} the godless.")

        name = name + " the godless"

        print(name)
        # the_vision()

    else:
        print("Oh? I haven't heard of that god. How boring, perhaps you should worship someone who is worthy.")

        print("Tell you what, I\'m going to pretend like "
               "I didn\'t hear that and we\'re going to start this conversation over. Deal?")

        print("Hopefully you could try to be more entertaining next time.")


start_choice()
Reply
#4
Addressing a couple of VizMark's comments:
  • The f before the string is a new feature called string interpolation, introduced in version 3.6
  • The claim that the original code needs to escape the single-quote in the contraction is false. This is easy to test empirically. If it used single-quotes instead of double-quotes, it would be true that a backslash would be needed for escaping, however there's no issue here.
Reply
#5
(Mar-21-2018, 05:58 PM)micseydel Wrote: This is a very common Python pitfall, please take a look at https://python-forum.io/Thread-Multiple-...or-keyword

Thanks for looking out. The link you sent was extremely helpful. Thanks!

(Mar-22-2018, 02:23 AM)VizMark Wrote: I'm also starting Python, already 2 weeks. So I'll try to help you out a little bit with your code.

print(f" ") I don't know the meaning of "f" before quotes. Besides that "f" your prints before else run well.

Issues_list:
  • From (if to last elif)your booleans are half right and half wrong. XD

I had the same mistake before, but after a good research I was still confused. So I decided to try adding the variable after the boolean.

If god == "King Behemoth" or "king behemoth": I could't make it run properly with a loop.
print("He is the mighty one!")


if god == "King Behemoth" or god == "king behemoth":
print("He is the mighty one!")

  • After (else:) all your print 29 to 31 lanes have issues with your contractions words, such as:

haven't
didn't
etc...

All contractions in python cannot run if you print them directly like: print("I can't understand"). In Python you need to use a backslash \ between the contraction and the apostrophe. print("I can\'t understand")

note: backslash (alt + 92) (\)
  • line 30 too long.



Sorry if I'm not good at explaining things because I'm still new with coding, but I'll try my best next time. Also i don't know if this is the result you wanted.

Hey VizMark, the f before the quotation marks while printing tells python to format the brackets within that print statement. It's a very easy and simple way to format things in my opinion.

The print statements on lines 29-31 work as intended. Had I started the print statement with a single quotation mark instead of the double quotations then you'd be correct. Or say if I wanted to include a double quote within that print statement, then I would need to include a backspace after it like you suggested. For instance:
print('I'll go run to the store.') .. That wouldn't work, but on the other hand
print("I'll go run to the store.") .. would work
print("Tommy said "ok".") wouldn't work either unless I included a backspace like you suggested after the quotation marks within the print statement.

Adding the variable after or like you did fixed my problems and worked perfectly though. Thanks a ton! Micseydel's link above also touched on that as well. Now everything works exactly as intended. Thanks for the reply :)
Reply
#6
Thx micseydel for the information and Bawhs all you said will help me a lot. And, good to know that the variable fixed your problem.
Reply
#7
Just lower the god's name.

if god.lower() == 'zeus':
    # bla
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

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