我正在尝试使turtle.right()
中的值成为对response = raw_input("")
的响应。这是代码:
print "Enter the number of degrees that you want .turtle to turn right"
choseDoor = False;
while choseDoor == False:
response = raw_input("Some suggestions are 1300, 179, 260, 59, 6400, 9999999, 123456789, 192837465, 150, 10 = 31415926, 11 = 1919, 12 = 126789\n")
if (response == "1") | (response == "one") | (response == "2") | (response == "two") | (response == "3") | (response == "three") | (response == "4") | (response == "four") | (response == "5") | (response == "five") | (response == "6") | (response == "six") | (response == "7") | (response == "seven") | (response == "8") | (response == "eight") | (response == "9") | (response == "nine") | (response == "10") | (response == "ten") | (response == "11") | (response == "eleven") | (response == "12") | (response == "twelve"):
choseDoor = True
print "this part of the script has been disabled. Please try again"
choseDoor = False
else:
val = "response"
import turtle
turtle.shape("turtle")
turtle.color("brown")
turtle.speed(99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999) #experement with speed
for i in range(9999999):
turtle.forward( i +5)
turtle.right(0 + "val")
#this part only goes in a straight line so far.
我的意图是使图形的角度值(turtle.right()
)为给定的响应。例如,如果我的响应为36
,则将运行turtle.right(36)
。
答案 0 :(得分:0)
因此,您需要将response
强制转换为int,然后可以在表达式中使用val。另外,您还可以在choseDoor
中恢复if
时切换False
。因为您要转换,所以我建议添加一个新的布尔值canCast
来查看响应是否可以转换为int,这样就可以摆脱长的if
表达式
print "Enter the number of degrees that you want .turtle to turn right"
choseDoor = False
canCast=True # new boolean
while choseDoor == False:
response = raw_input("Enter the number of degrees that you want .turtle to turn right:")
try:
response=int(response)
except:
canCast=False #can't cast response must be a string
if not canCast:
print "this part of the script has been disabled. Please try again"
canCast=True #reset the canCast flag
else:
val = int(response) # cast response to int
import turtle
turtle.shape("turtle")
turtle.color("brown")
turtle.speed(99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999) #experement with speed
for i in range(9999999):
turtle.forward( i +5)
turtle.right(val)
答案 1 :(得分:0)
乌龟角为float
,所以我用它代替了@depperm解决方案中的int
; 10 = 31415926, 11 = 1919, 12 = 126789
看起来像预定义的角度,所以我把它们扔了进去;我添加了一个“退出”选项; turtle.speed(99999...99999)
没有意义,只有值0-10有效,因此我将其切换为备用"fastest"
参数格式; forward(9999999 + 5)
似乎过多,因此我将其降至100;我添加了一些逻辑,以便连续两个不同角度的输入将绘制同心对象并在它们之间举起笔:
import turtle
predefined = {10: 31415926, 11: 1919, 12: 126789}
print("Enter the angle in degrees that you want the turtle to turn right")
while True:
response = raw_input("Some suggestions are 1300, 179, 260, 59, 6400, 9999999, 123456789, 192837465, 150, 10 = 31415926, 11 = 1919, 12 = 126789\n")
if response.lower() == 'exit':
break
elif response in predefined:
angle = predefined[response]
else:
try:
angle = float(response)
except ValueError:
print("this part of the script has been disabled. Please try again")
continue
turtle.shape('turtle') # do this late so open turtle window after prompts
turtle.speed('fastest')
turtle.color('brown')
turtle.home() # for drawings after initial one
turtle.pendown()
for i in range(100):
turtle.forward(i + 5)
turtle.right(angle)
turtle.penup()