鱼C论坛

 找回密码
 立即注册
查看: 1784|回复: 3

[已解决]int和int(str)为何不能呢

[复制链接]
发表于 2017-4-3 10:27:44 | 显示全部楼层 |阅读模式

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

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

x
#windes10的python3.6

  1. def p():
  2.    
  3.     DaysPerYear = 365
  4.     HoursPerDay = 24
  5.     MinutesPerHour = 60
  6.     SecondsPerMinue = 60
  7.     a = int('DaysPerYear') * int('HoursPerDay') * ('MinutesPerHour') * ('SecondsPerMinue')
  8.     print('今年一年一共有',a, '秒')

  9. p()   
  10. #报错
复制代码


为何会出现这种情况呢?还有这一个

  1. DaysPerYear = input('一年一共有多少天?')
  2. HoursPerDay = input('一天多少小时?')
  3. MinutesPerHour = 60
  4. SecondsPerMinue = 60
  5. def p():
  6.       
  7.     a = int(DaysPerYear*HoursPerDay*MinutesPerHour*SecondsPerMinue)
  8.     print('今年一年一共有',a, '秒')

  9. p()   
  10. #报错
复制代码

最佳答案
2017-4-3 10:35:05
DaysPerYear  HoursPerDay  MinutesPerHour SecondsPerMinue 都是一个变量名称,而你再引用的时候用的是字符串 正确的做法是int(DaysPerYear) int(HoursPerDay) int(MinutesPerHour)
int(SecondsPerMinue) 当你尝试将一个变量转换类型,用的都是变量的变量名,而不是跟变量名一样的字符串
int('DaysPerYear')这样无法转换是因为,字符串中都是英文字母,因为字母没有对应的整形数
int(DaysPerYear*HoursPerDay*MinutesPerHour*SecondsPerMinue)这一句不成功是因为你的变量中有字符串也有整形,两个字符串之间无法相乘
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-3 10:35:05 | 显示全部楼层    本楼为最佳答案   
DaysPerYear  HoursPerDay  MinutesPerHour SecondsPerMinue 都是一个变量名称,而你再引用的时候用的是字符串 正确的做法是int(DaysPerYear) int(HoursPerDay) int(MinutesPerHour)
int(SecondsPerMinue) 当你尝试将一个变量转换类型,用的都是变量的变量名,而不是跟变量名一样的字符串
int('DaysPerYear')这样无法转换是因为,字符串中都是英文字母,因为字母没有对应的整形数
int(DaysPerYear*HoursPerDay*MinutesPerHour*SecondsPerMinue)这一句不成功是因为你的变量中有字符串也有整形,两个字符串之间无法相乘
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-3 10:35:05 | 显示全部楼层

回帖奖励 +1 鱼币

- -卡了下重复了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-3 11:17:43 | 显示全部楼层
lumber2388779 发表于 2017-4-3 10:35
DaysPerYear  HoursPerDay  MinutesPerHour SecondsPerMinue 都是一个变量名称,而你再引用的时候用的是字 ...

正解,要区分字符串 和 数字
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-22 16:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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