I'm a beginner in Python and I'm struggling to find my mistake. I have 3, now two conditions for input, and not sure what the problem is. I have tried implementing in various ways, from using all if statements then if/else, then nesting as well as while loop. I'm not sure what I'm doing wrong.
def switch_off():
robot_name
answer = input (robot_name ": what must I do next? " )
if answer == "OFF":
quit()
if answer.lower() != "off":
print( robot_name ": Shutting down...")
if answer== "OFF" or answer.lower() != "off":
print(robot_name ": Sorry, I did not understand")
answer = input (robot_name ": what must I do next? " )
CodePudding user response:
Did you call the function at the end of your code? Also you need to input a value for your 'robot_name' variable!
You can call the function by typing this at the end of your code:
switch_off()
*PS. I know this isn't what you asked, but your if-else statements give very odd returns and should be optimized!
For example, when I input 'k' into the console, it returns this:
Bob: what must I do next? k
Bob: Shutting down...
Bob: Sorry, I did not understand
Bob: what must I do next?
You can fix this by making sure your if-else statements dont overlap each other!
Hopefully this helps :)
CodePudding user response:
I really don't know what you really want but try this
using while loop you can loop through and give orders.
def switch_off():
robot_name
answer = input(robot_name ": what must I do next? ")
while True:
if answer == "OFF":
quit()
elif answer.lower() != "off":
print( robot_name ": Shutting down...")
break
elif answer=="OFF" or answer.lower() != "off":
print(robot_name ": Sorry, I did not understand")
answer = input (robot_name ": what must I do next? " )
break
CodePudding user response:
I don't know what your end goal is, but I hope this helps.
To run this function, you to call it. I added "switch_off()" at the end of the code.
We will encounter an error (NameError) because "robot_name" is not defined, meaning it's not set equal to anything. In my example code below I set it equal to "AI". Now your code will run.
I am not sure what your intentions are, but I will explain my example code below (hopefully it'll help). When you run the switch_off function, it will prompt you with "AI: what must I do next?". If you type in "OFF", it will quit the program. If you don't type "OFF", but type "off" then the program will print "AI: Shutting down...". And if you type something other than "OFF" or "off", it will repeat the function by calling it again (see how I have switch_off in the "else:")
Note that == means equal, and != means not equal. Also keep in mind that "else:" will always run if the previous if and/or elif aren't true.
def switch_off():
robot_name = "AI"
answer = input (robot_name ": what must I do next? " )
if answer == "OFF":
quit()
elif answer.lower() == "off":
print( robot_name ": Shutting down...")
else:
print(robot_name ": Sorry, I did not understand")
switch_off()
switch_off()
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>