sorry I am a bit of a beginner and I am having trouble gettin the else statement working properly in this program i made to draw stuff in turtle with python. I tried indenting the else statementelse: print("Wrong input!")
but then it just says "Wrong input!" Everytime i do something; the program works fine but that keeps repeating. If i keep the else statement like that and I type anything else then the "Wrong input!" that is supposed to pop up doesn't appear. Anyone know what to do?
For example: If i run the code like how i gave it here this is how it turns out:
what you want to do?: f (<-- I input this)
How many pixels you want to go forward?: 100 (<-- I input this)
and now if i put something else
what you want to do?: asbfaifb (<-- I input this)
what you want to do?:
it doesnt show the "Wrong input!" message that is supposed to say
On the other hand if I indent the else statement one more time this is the outcome
What you want to do?: f
How many pixels you want to go forward?: 100
Wrong input!
What you want to do?: r
How many degrees you want to turn right?: 90
Wrong input!
What you want to do?: 100
Wrong input!
What you want to do?: b
Wrong input!
What you want to do?:
but the code still runs fine, it just keeps repeating the "Wrong input!" over and over
#importing turle
import turtle
#Creating our turtle and naming it
turtle = turtle.Turtle()
#Ask the user what to do and tell the instructions
#Make a function for the instructions
def instructions():
print("What do you want me to do? Please choose the letter corresponding to the action: ")
print("Forward = f")
print("Turn right = r")
print("Turn left = l")
print("Turn backwards = b")
print("If you want to stop: stop\n")
#print out the instuctions
instructions()
#Function for drawing by getting the user's input
def drawing():
while True:
user = input("What you want to do?: ")
#If they want to go forward
if user == "f":
l = int(input("How many pixels you want to go forward?: "))
turtle.forward(l)
#If they want to turn right
elif user == "r":
x = int(input("How many degrees you want to turn right?: "))
turtle.right(x)
#If they want to turn left
elif user == "l":
y = int(input("How many degrees you want to turn left?: "))
turtle.left(y)
#If they want to turn backwards
elif user == "b":
z = 180
turtle.right(z)
#If they want to stop the program
if user == "stop":
print("\nOk, I will stop the program now\n\nTo make a new drawing, re-run the program")
break
#If they type anything else
else:
print("Wrong input!")
drawing()
CodePudding user response:
else
follows the last if
or elif
statement before it. For you, this is:
if user == "stop":
Which means, if user is not "stop", you are telling the program to print "wrong input".
An easy fix is to simply change if user == "stop":
to elif user == "stop":