PYTHON Def function prints itself? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

23rd Aug 2017, 8:18 AM
Markus Beyer
Markus Beyer - avatar
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"]:
24th Aug 2017, 3:59 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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"
25th Aug 2017, 12:45 AM
Daemo
Daemo - avatar
+ 2
Would be glad to help, but can I see the code please ?
23rd Aug 2017, 9:20 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
do you have a game loop? are you checking for input before proceeding to the next decision point?
23rd Aug 2017, 9:59 AM
Daemo
Daemo - avatar
+ 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
23rd Aug 2017, 10:59 AM
Markus Beyer
Markus Beyer - avatar
+ 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
24th Aug 2017, 4:01 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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
24th Aug 2017, 4:02 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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:") ?
24th Aug 2017, 4:19 PM
Markus Beyer
Markus Beyer - avatar
0
https://code.sololearn.com/cOHgM8dfJMLi/?ref=app That's the code, please help ^^
24th Aug 2017, 2:26 PM
Markus Beyer
Markus Beyer - avatar
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() ....
24th Aug 2017, 4:22 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
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?
24th Aug 2017, 4:37 PM
Markus Beyer
Markus Beyer - avatar
0
if your code is only : start = input("Some text :") The only output on sololearn should be : Some text :
24th Aug 2017, 4:40 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
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)
24th Aug 2017, 4:56 PM
Markus Beyer
Markus Beyer - avatar
0
add print(start) and you'll see that start value was not printed
24th Aug 2017, 5:03 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
Add where?
24th Aug 2017, 6:17 PM
Markus Beyer
Markus Beyer - avatar
0
And lastly I think my indentations are right.. Deleted the or's in the code too, still doesn't wodk
24th Aug 2017, 9:30 PM
Markus Beyer
Markus Beyer - avatar