如何在屏幕(乌龟)上而不是在终端上向用户显示问题?
How do I display questions to the user on a screen (turtle), instead of in the terminal?
我正在编写一个程序来解决 f.e。二次公式。
但是我想在带有“import turtle”的屏幕中而不是在终端中询问变量(A,B,C)。
我知道如何制作屏幕:背景颜色;像素数量;等等
但是我不知道如何询问变量并在那个屏幕上显示答案。
这是代码:
import math
import turtle
wn = turtle.Screen()
wn.title("VKV by @Boldarcticwolf")
wn.bgcolor("green")
wn.setup(width=600, height=600)
A = int(input("What is A? "))
B = int(input("What is B? "))
C = int(input("What is C? "))
D = (B * B) - (4 * A * C)
if D < 0:
print('D is', D)
elif D == 0:
X1 = (-1 * B) / (2 * A)
print('D is 0 and x is', X )
elif D > 0:
X1 = ((-1 * B) + math.sqrt(D)) / (2 * A)
X2 = ((-1 * B) - math.sqrt(D)) / (2 * A)
print('D is', D, ', X1 is', X1, 'and X2 is', X2)
这会打开一个屏幕并询问变量,但不会在屏幕上询问它们。
您可以使用 turtle.textinput 弹出对话框供用户输入。
import math
import turtle
wn = turtle.Screen()
wn.title("VKV by @Boldarcticwolf")
wn.bgcolor("green")
wn.setup(width=600, height=600)
A = int(turtle.textinput('A value',"What is A? "))
B = int(turtle.textinput('B value',"What is B? "))
C = int(turtle.textinput('C value',"What is C? "))
D = (B * B) - (4 * A * C)
if D < 0:
turtle.write(f'D is{D}', font=('Courier', 30, 'italic'), align='center' )
elif D == 0:
X1 = (-1 * B) / (2 * A)
turtle.write(f'D is 0 and {X1} is {X1}', font=('Courier', 30, 'italic'), align='center' )
elif D > 0:
X1 = ((-1 * B) + math.sqrt(D)) / (2 * A)
X2 = ((-1 * B) - math.sqrt(D)) / (2 * A)
turtle.write(f'D is {D}, X1 is{X1} and and X2 is {X2}', font=('Courier', 30, 'italic'), align='center' )
wn.exitonclick()
注意,您可能应该添加某种验证,或使用 try/except,以检查用户是否实际输入了数值。
我正在编写一个程序来解决 f.e。二次公式。 但是我想在带有“import turtle”的屏幕中而不是在终端中询问变量(A,B,C)。 我知道如何制作屏幕:背景颜色;像素数量;等等 但是我不知道如何询问变量并在那个屏幕上显示答案。
这是代码:
import math
import turtle
wn = turtle.Screen()
wn.title("VKV by @Boldarcticwolf")
wn.bgcolor("green")
wn.setup(width=600, height=600)
A = int(input("What is A? "))
B = int(input("What is B? "))
C = int(input("What is C? "))
D = (B * B) - (4 * A * C)
if D < 0:
print('D is', D)
elif D == 0:
X1 = (-1 * B) / (2 * A)
print('D is 0 and x is', X )
elif D > 0:
X1 = ((-1 * B) + math.sqrt(D)) / (2 * A)
X2 = ((-1 * B) - math.sqrt(D)) / (2 * A)
print('D is', D, ', X1 is', X1, 'and X2 is', X2)
这会打开一个屏幕并询问变量,但不会在屏幕上询问它们。
您可以使用 turtle.textinput 弹出对话框供用户输入。
import math
import turtle
wn = turtle.Screen()
wn.title("VKV by @Boldarcticwolf")
wn.bgcolor("green")
wn.setup(width=600, height=600)
A = int(turtle.textinput('A value',"What is A? "))
B = int(turtle.textinput('B value',"What is B? "))
C = int(turtle.textinput('C value',"What is C? "))
D = (B * B) - (4 * A * C)
if D < 0:
turtle.write(f'D is{D}', font=('Courier', 30, 'italic'), align='center' )
elif D == 0:
X1 = (-1 * B) / (2 * A)
turtle.write(f'D is 0 and {X1} is {X1}', font=('Courier', 30, 'italic'), align='center' )
elif D > 0:
X1 = ((-1 * B) + math.sqrt(D)) / (2 * A)
X2 = ((-1 * B) - math.sqrt(D)) / (2 * A)
turtle.write(f'D is {D}, X1 is{X1} and and X2 is {X2}', font=('Courier', 30, 'italic'), align='center' )
wn.exitonclick()
注意,您可能应该添加某种验证,或使用 try/except,以检查用户是否实际输入了数值。