I'm not sure what in my code is wrong but it doesn't return the print statements. Any help is appreciated. edit 1: Adding firstpath(input1) vs firstpath() at the end does nothing edit 2: thanks gene for the correct response! print("Welcome to (insert title here)\n\n") input1 = input("You are walking down a street, and suddenly hear a loud bang. Type Y to investigate or N to ignore: ") def firstpath(input1): if input1 == "Y": print("You walk towards the sound") elif input1 == "N": print("You continue down your path") firstpath()
4 Answers
Your code maybe should've been:
print("Welcome to (insert title here)\n\n")
input1 = input("You are walking down a street, and suddenly hear a loud bang. Type Y to investigate or N to ignore: ").lower()
def firstpath(local_input):
if local_input == "y":
print("You walk towards the sound")
elif local_input == "n":
print("You continue down your path")
else:
print('Sorry, bad answer.')
firstpath(input1)
Comments
Call firstpath() outside the function definition.
You should also add else condition to your code for it to work properly.!
print("Welcome to (insert title here)\n\n")
input1 = input("You are walking down a street, and suddenly hear a loud bang. Type Y to investigate or N to ignore: ")
def firstpath(input1):
if input1 == "Y":
print("You walk towards the sound")
elif input1 == "N":
print("You continue down your path")
else:
print("Please enter either Y or N")
firstpath(input1)
10 Comments
CranberryJuice
still did nothing
CranberryJuice
the added else worked, thank you!!! I don't get what I was doing wrong tho
theX
@CranberryJuice The
input1 global variable and the input1 local variable is causing some ambiguity.CranberryJuice
But how else would that work? Should I put input statements inside my function ?
theX
@CranberryJuice Else means everything else. Like if the top two were
False |
print("Welcome to (insert title here)\n\n")
input1 = input("You are walking down a street, and suddenly hear a loud bang. Type Y to investigate or N to ignore: ")
def firstpath(input1):
if input1 == "Y":
print("You walk towards the sound")
elif input1 == "N":
print("You continue down your path")
firstpath(input1)
1 Comment
CranberryJuice
Traceback (most recent call last): File "C:/Users/kmd20/Desktop/Python Projects/textbasedadventure/gam.py", line 8, in <module> firstpath() TypeError: firstpath() missing 1 required positional argument: 'input1'
You are calling firstpath(input1) within the function. Try this:
print("Welcome to (insert title here)\n\n")
input1 = input("You are walking down a street, and suddenly hear a loud bang. Type Y to investigate or N to ignore: ")
def firstpath(input1):
if input1 == "Y":
print("You walk towards the sound")
elif input1 == "N":
print("You continue down your path")
firstpath(input1)
2 Comments
CranberryJuice
nope doesn't work, just ignores everything and gives me Process finished with exit code 0
11thal11thal
It worked for me--maybe this has something to do with the version of python you are running it in?
firstpath(input1)anywhere? Try adding that after the code.firstpath()at the end is within the function definition. Try tabbing that back out and includinginput1as a parameter of the function as suggested above.