鱼C论坛

 找回密码
 立即注册
查看: 1706|回复: 7

[已解决]python不知道为什么报错

[复制链接]
发表于 2017-10-30 19:17:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
temp=input("请输入一个年份")
while not temp.isdigit():
    print("输入格式不正确,请再输入一个年份:")
    temp=input()
year=int(temp)
if year/4 == 0 and year/100 != 0:
    print(temp"是闰年")
else:
    if year/400 == 0:
        print(temp"是闰年")
    else:
        print(temp"不是闰年")
以上是代码原文,截图也附在图片中

这是报错点

这是报错点

这是报错原因

这是报错原因
最佳答案
2017-10-30 19:30:49
  1. temp=input("请输入一个年份:")
  2. while not temp.isdigit():
  3.     print("输入格式不正确,请再输入一个年份:")
  4.     temp=input()
  5. year=int(temp)
  6. if year%4 == 0 and year%100 != 0:
  7.     print('%d是闰年'%year)
  8. else:
  9.     if year%400 == 0:
  10.         print("%d是闰年"%year)
  11.     else:
  12.         print("%d不是闰年"%year)
复制代码

应该这样写吧,这样写是可以的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-10-30 19:30:49 | 显示全部楼层    本楼为最佳答案   
  1. temp=input("请输入一个年份:")
  2. while not temp.isdigit():
  3.     print("输入格式不正确,请再输入一个年份:")
  4.     temp=input()
  5. year=int(temp)
  6. if year%4 == 0 and year%100 != 0:
  7.     print('%d是闰年'%year)
  8. else:
  9.     if year%400 == 0:
  10.         print("%d是闰年"%year)
  11.     else:
  12.         print("%d不是闰年"%year)
复制代码

应该这样写吧,这样写是可以的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-30 21:27:08 | 显示全部楼层
print用法错误
print(temp"是闰年")
这里要么改成
print(temp + "是闰年")
要么改成
print("%s是闰年" % temp)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-10-31 09:11:18 | 显示全部楼层
皮皮小发 发表于 2017-10-30 19:30
应该这样写吧,这样写是可以的

谢谢,可以啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-31 09:11:53 | 显示全部楼层
BngThea 发表于 2017-10-30 21:27
print用法错误
print(temp"是闰年")
这里要么改成

明白了,谢谢您
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-31 09:44:30 | 显示全部楼层
BngThea 发表于 2017-10-30 21:27
print用法错误
print(temp"是闰年")
这里要么改成

为什么要用%s啊,这是在干什么啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-31 09:44:45 | 显示全部楼层
BngThea 发表于 2017-10-30 21:27
print用法错误
print(temp"是闰年")
这里要么改成

为什么要用%s啊,这是在干什么啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-31 10:33:34 | 显示全部楼层
戈戈戈戈戈戈哒 发表于 2017-10-31 09:44
为什么要用%s啊,这是在干什么啊

这个叫格式化输出,你可以查看
http://bbs.fishc.com/forum.php?m ... peid%26typeid%3D403
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-3 13:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表