设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

集中答疑专用贴

  [复制链接]

0

主题

0

好友

6

积分

新手上路

Rank: 1

楼主
发表于 2017-8-2 18:32:57 来自手机 |显示全部楼层
num1 = 0
num2 = 0

direction = ['Left','Center','Right']

for i in range(5):
    print(" ==== Round %d, - You Kick! ====\n"%(i+1))
    print("Choose one side to shoot!\n")
    print("'Left','Center','Right'\n")
    you = input()
    print('You kicked '+you)
    com = choice(direction)
    print('Com saved '+com)
    if you != com:
        print('Goal!\n')
        num1 += 1
    else:
        print('Oops!\n')
        num2 += 1
    print("Score: %d(you):%d(com)\n"%(num1,num2)
   
    print("==== Round %d - You save! ====\n"%(i+1))
    print('Choose one side to save!\n')
    print("'Left','Center','Right'\n")
    you = input()
    print("You saved " + you)
    com = choice(direction)
    print("Com Kicked " + com)
    if you == com:
        print("Saved!\n")
        num1 += 1
    else:
        print("Oops!\n")
        num2 += 1
    print("Score: %d(you) : %d(com)\n"%(num1,num2)

报错:
  File "/storage/emulated/0/qpython/project/.last_tmp.py", line 59
    print("==== Round %d - You save! ====\n"%(i+1))
        ^
SyntaxError: invalid syntax
回复

使用道具 举报

0

主题

0

好友

6

积分

新手上路

Rank: 1

沙发
发表于 2017-8-2 19:52:46 来自手机 |显示全部楼层
crossin先生 发表于 2017-8-2 19:02
这句话的前一行少了半个括号

谢谢您的解决方法,的确是那里出错。
还要叨扰下。

from random import choice
def Kick():
    print("==== Round %d, - You Kick! ===="%(i+1))
    print("Choose one side to shoot!")
    print("'Left','Center','Right'")
    you = input()
    print('You kicked '+you)
    com = choice(direction)
    print('Com saved '+com)
    if you != com:
        print('Goal!')
        num1 += 1
        return()
    else:
        print('Oops!')
        num2 += 1
        return()
    print("Score: %d(you):%d(com)"%(num1,num2))

def Save():
    print("==== Round %d - You save!===="%(i+1))
    print("Choose one side to save!")
    print("'Left','Center','Right'")
    you = input()
    print("You saved " + you)
    com = choice(direction)
    print("Com Kicked " + com)
    if you == com:
        print("Saved!")
        num1 += 1
        return()
    else:
        print("Oops!")
        num2 += 1
        return()
    print("Score: %d(you) : %d(com)"%(num1,num2))

num1 = 0
num2 = 0

direction = ['Left','Center','Right']

for i in range(5):
    Kick()

    Save()

报错:
Traceback (most recent call last):
  File "/storage/emulated/0/qpython/project/.last_tmp.py", line 44, in <module>
    Kick()
  File "/storage/emulated/0/qpython/project/.last_tmp.py", line 12, in Kick
    num1 += 1
UnboundLocalError: local variable 'num1' referenced before assignment

麻烦了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-7 07:27 , Processed in 0.029317 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部