设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 5124|回复: 2
打印 上一主题 下一主题

python入门第40课中出现“未终止的字符串文字”...

[复制链接]

2

主题

0

好友

24

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2021-11-17 15:17:12 |只看该作者 |倒序浏览
本帖最后由 小刘 于 2021-11-17 15:20 编辑

代码如下:
import random
key = random.randint(0,100)
num = key

f = open("gamescore.txt")
score = f.read().split()
f.close()

game_times = int(score[0])
min_times = int(score[1])
total_times = int(score[2])

if game_times > 0:
    avg_times = total_times / game_times
else:
    avg_times = 0

print("You already played %d times, guess the answer at least %d round, guess the answer in an average of %.2f rounds
" % (game_times, min_times, avg_times))

print("Guess a number.")
bingo = False

while bingo == False:
    times += 1
    answer = int(input())

    if answer < num:
        print("Too small!")

    if answer > num:
        print("Too big!")

    if answer == num:
        print("BINGO! %s is the right answer!"%num)
        bingo = True

if game_times == or times < min_times:
    min_times = times
total_times += times
game_times += 1
result = "%d %d %d" % (game_times, min_times, total_times)
f = open("gamescore.txt", "w")
f.write(result)
f.close()

跑代码时出现“第18行中未终止的字符串文字”的反馈
希望能有大神指点指点

回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

沙发
发表于 2021-11-17 23:49:54 |只看该作者
  1. print("You already played %d times, guess the answer at least %d round, guess the answer in an average of %.2f rounds" % (game_times, min_times, avg_times))
复制代码
这是一行代码,因为过长而在网页上显示为两行,你不要人为地分成两行
如果要分行,你也在引号之后 %之前换行,不然你换在了引号之内,硬生生把一个字符串跨行了,不符合语法
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

2

主题

0

好友

24

积分

新手上路

Rank: 1

板凳
发表于 2021-11-18 08:45:37 |只看该作者
crossin先生 发表于 2021-11-17 23:49
这是一行代码,因为过长而在网页上显示为两行,你不要人为地分成两行
如果要分行,你也在引号之后 %之前换 ...

噢噢噢噢哦哦哦 明白了 感谢crossin先生
回复

使用道具 举报

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

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

GMT+8, 2024-4-19 11:57 , Processed in 0.018081 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部