Ada_Kingle 2022-03-03 21:53 采纳率: 100%
浏览 45
已结题

python基础问题求解

问题遇到的现象和发生背景

img

问题相关代码,请勿粘贴截图

import turtle as t
color = ['green','red','blue']
r = ['20','50','60']
for i in range(3):
t.pu()
t.goto(0,-r[i])
t.pd()
t.pencolor(color[i])
t.circle(r[i])
t.done()

运行结果及报错内容

File "C:\pycharm\武球王.py", line 7, in
t.goto(0,-r[i])
TypeError: bad operand type for unary -: 'str'

我的解答思路和尝试过的方法

我尝试过将减号去掉,但是没用,依旧报错

我想要达到的结果

正确运行

  • 写回答

1条回答 默认 最新

  • 谛凌 2022-03-03 21:57
    关注
    import turtle as t
    color = ['green','red','blue']
    r = ['20','50','60']
    for i in range(3):
        t.pu()
        t.goto(0,-eval(r[i]))
        t.pd()
        t.pencolor(color[i])
        t.circle(r[i])
    t.done()
    

    或者:

    import turtle as t
    color = ['green','red','blue']
    r = [20,50,60]
    for i in range(3):
        t.pu()
        t.goto(0,-r[i])
        t.pd()
        t.pencolor(color[i])
        t.circle(r[i])
    t.done()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 3月15日
  • 已采纳回答 3月7日
  • 创建了问题 3月3日