使响应成为 `turtle.right()` 中的角度

Making the response the angle in `turtle.right()`

我正在尝试使 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()) 成为给定的响应。例如,如果我的回复是 36turtle.right(36) 将是 运行。

所以你需要将response转换为int然后你可以在表达式中使用val。您还可以删除 ifchoseDoor 的切换,因为它恢复为 False。因为你正在投射我建议添加一个新的布尔值 canCast 以查看响应是否可以转换为 int,然后你可以摆脱 long 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)

海龟角度是 float 所以我用它代替了 @depperm 解决方案中的 int10 = 31415926, 11 = 1919, 12 = 126789 看起来像预定义的角度,所以我把它们扔进去了;我添加了一个 'exit' 选项; 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()