Alex
Alex

Reputation: 15

getting more than 1 value from turtle.textinput()

I know that the following code will give 2 variables

var1,var2 = input("Input 2 things: ").split()

but I am using turtle and to have an input in the turtle screen is with the method textinput(). I have this line of code, thinking that it would work:

var1,var2 = turtle.textinput("Input 2 things: ").split()

I'd type in "3 5" expecting var1 == 3 and var2 == 5 but instead I get, note that I have no idea what the entire error is about except for the last line.

Exception in Tkinter callback

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/init.py", line 1699, in call return self.func(*args) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/turtle.py", line 675, in eventfun fun(x, y)
NameError: name 'var2' is not defined

Could someone explain how to get 2 variables from one input in the turtle screen. I only know of the turtle.textinput to get the text to appear in turtle.

Upvotes: 0

Views: 1456

Answers (2)

Ahmouse
Ahmouse

Reputation: 195

The split method allows you to choose which symbol to split by, so what you could try is to split by the space symbol, for example:

var1,var2 = turtle.textinput("Input 2     things: ").split(' ')

so if I input 3 5 then var1 will equal to 3 and var2 will equal to 5.

Upvotes: 0

cdlane
cdlane

Reputation: 41905

turtle.textinput() isn't a direct replacement for input() as it takes a another, initial argument, in addition to the prompt. It requires a title for the dialog window:

> python3
...
>>> import turtle
>>> var1,var2 = turtle.textinput("User Input", "Input 2 things: ").split()
>>> var1
'3'
>>> var2
'5'
>>> 

Upvotes: 4

Related Questions