0
PYTHON Def function prints itself?
I am coding a multiple choice story but all the possible choices just got printed as they are defined one after another, my order doesn't work. What could be the problem or what may I do wrong? https://code.sololearn.com/cOHgM8dfJMLi/?ref=app
16 Answers
+ 2
The big problem is :
if second2() == "Knife" or "knife":
If I had parenthesis to show you how the computer interpret this line, it will give :
if (second2() == "Knife") or "knife":
Moreover, a none empty string is considered true so all your conditions are true. The solution could be one of those two :
solution 1 :
choice = second2()
if choice == "Knife" or choice == "knife":
solution 2 :
if second2() in ["Knife","knife"]:
+ 3
https://code.sololearn.com/csC9nIM29565/?ref=app
I've cleaned up your tabs and removed a heap of extra spaces.
works better for me but I have only tested with input "knife"
+ 2
Would be glad to help, but can I see the code please ?
+ 2
do you have a game loop?
are you checking for input before proceeding to the next decision point?
+ 1
Can't post the code now, will do later for sure. Ain't got a loop, yes the function which needs decisions asks for input
+ 1
Secondly, python define a block by its indentation, so in
def f(a):
a+=1
if a == 2:
print(a)
the if statement is NOT in the function, it will be called when read
Last problem I spotted is that you forgot parenthesis sometimes when you called start
+ 1
One last (I forgot it) problem is the start function
you do not store the input and the start function (once correctly indented) will call itself infinitely
+ 1
Hey there thanks for all the help so far, I will soon try your Suggestions on the code. But why do my variables print itself immediately?
Start=input("Enter stuff:")
?
0
https://code.sololearn.com/cOHgM8dfJMLi/?ref=app
That's the code, please help ^^
0
First because start is the name of the function, and second because the line you wrote here is not in your code :)
def start():
input()
if start() ....
0
Okay okay haha but I mean in another code and even before I used start as a function in this one, I tried using it as a variable, but it didn't work it just printed itself as it was created, very weird, any idea?
0
if your code is only :
start = input("Some text :")
The only output on sololearn should be :
Some text :
0
Yes it is, but shouldn't I have to call it first?
The problem is, as I define it, it directly gets printed without me actually coding
print(start)
0
add
print(start)
and you'll see that start value was not printed
0
Add where?
0
And lastly I think my indentations are right.. Deleted the or's in the code too, still doesn't wodk