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

时间:2019-05-22 19:17:27

标签: python python-2.7 turtle-graphics

我正在尝试使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)

2 个答案:

答案 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解决方案中的int10 = 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()