Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将响应设置为`海龟.右()中的角度`_Python_Python 2.7_Turtle Graphics - Fatal编程技术网

Python 将响应设置为`海龟.右()中的角度`

Python 将响应设置为`海龟.右()中的角度`,python,python-2.7,turtle-graphics,Python,Python 2.7,Turtle Graphics,我试图在turtle.right()中生成对response=raw\u输入(“”的响应中的值。代码如下: print“输入所需的度数。海龟右转” choseDoor=假; 而choseDoor==False: 响应=原始输入(“一些建议是1300、179、260、59、6400、9999999、123456789、192837465、150、10=31415926、11=1919、12=126789\n”) 如果(反应=“1”)(反应=“1”)(反应=“2”)(反应=“2”)(反应=“3”)(

我试图在
turtle.right()
中生成对
response=raw\u输入(“”
的响应中的值。代码如下:

print“输入所需的度数。海龟右转”
choseDoor=假;
而choseDoor==False:
响应=原始输入(“一些建议是1300、179、260、59、6400、9999999、123456789、192837465、150、10=31415926、11=1919、12=126789\n”)
如果(反应=“1”)(反应=“1”)(反应=“2”)(反应=“2”)(反应=“3”)(反应=“3”)(反应=“3”)(反应=“4”)(反应=“4”)(反应=“4”)(反应=“5”)(反应=“五”)(反应=“6”)(反应=“六”)(反应=“七”)(反应=“七”)(反应=“八”)(反应=“八”)(回应==“9”)|(回应==“9”)|(回应==“10”)|(回应==“10”)|(回应==“11”)|(回应==“11”)|(回应==“12”)|(回应==“12”):
choseDoor=True
打印“此脚本部分已被禁用。请重试”
choseDoor=False
其他:
val=“响应”
进口海龟
海龟形状(“海龟”)
乌龟。颜色(“棕色”)
乌龟。速度(9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999)#速度体验
对于范围内的i(999999):
乌龟前进(i+5)
海龟。右(0+“val”)
#到目前为止,这部分只是直线。

我的目的是使绘图的角度值(
turtle.right()
)成为给定的响应。例如,如果我的响应是
36
turtle.right(36)
将运行。

因此您需要将
响应
强制转换为int,然后您可以在表达式中使用val。此外,您还可以在
if
中删除
choseDoor
的切换,因为它会恢复为
False
。因为您正在强制转换,我建议添加一个新的布尔值
canCast
,以查看响应是否为c可以将一个表达式转换为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)

海龟角度是
float
,所以我用它来代替@depperm的解决方案中的
int
;10=31415926,11=1919,12=126789看起来像预定义的角度,所以我把它们放进去了;我添加了一个“退出”选项;
Turtle.speed(99999…99999)
没有任何意义,只有值0-10起作用,因此我将其切换为备用
“最快”
参数格式;
向前(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()