所以,我对python 3.5.0很新,最近我一直在研究睡眠计算器,但是我遇到了一个问题,这是我的代码到目前为止......它已经注释了标签/注释:
#sleep calculator, user enters there hours slept over a nicght and the program
#will work out different facts about there sleaping
print("Welcome to the sleep calculator, all you have to do is answer one question...")
hourspernight = int(input("how many hours per night do you sleep?"))
#variable for the hours per week slept
hoursperweek = hourspernight * 7
#telling the user how many hours er week they sleep
print ("you sleep", hoursperweek,"hours per week")
#variable for how many hours per month they sleep
hourspermonth = float(hoursperweek * 4.35)
#teling the user how namy hours per month they sleep
hourspermonth = print("you also sleep", hourspermonth,"hours per month")
#ISSUE, this is the variable that python has a problem with, and I'm not sure why
dayspermonth = (hourspermonth) / (hourspernight) * 24
#telling the user how many days per month they sleep (this bits ok... I think)
dayspermonth = print("you sleep for",dayspermonth,"days per month aswell!")
错误:
========================= RESTART: F:/sleep hours.py
=========================
Welcome to the sleep calculator, all you have to do is answer one question...
how many hours per night do you sleep?8
you sleep 56 hours per week
you also sleep 243.59999999999997 hours per month
Traceback (most recent call last):
File "F:/sleep hours.py", line 14, in <module>
dayspermonth = (hourspermonth) / (hourspernight) * 24
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
更新
它不再出错,但每月的日子看起来不是正确的答案,这是正确的吗?
========================= RESTART:F:/ sleep hours.py ============= ============
Welcome to the sleep calculator, all you have to do is answer one question...
how many hours per night do you sleep?8
you sleep 56 hours per week
you also sleep 243.59999999999997 hours per month
you sleep for 730.8 days per month aswell!
更新#2
一切都运行良好,但我不确定每月的计算天数,这里是代码行
dayspermonth = (hourspermonth) / (hourspernight) * 24
如果有人能告诉我这是否正确。
更新#3
现在使用的代码:
# sleep calculator, user enters there hours slept over a nicght and the program
# will work out different facts about there sleaping
print("Welcome to the sleep calculator, all you have to do is answer one question...")
hourspernight = int(input("how many hours per night do you sleep?"))
# variable for the hours per week slept
hoursperweek = hourspernight * 7
# telling the user how many hours er week they sleep
print ("you sleep", hoursperweek,"hours per week")
# variable for how many hours per month they sleep
hourspermonth = float(hoursperweek * 4.35)
# teling the user how namy hours per month they sleep
print("you also sleep", "{0:.2f}".format(hourspermonth),"hours per month")
# variable for calculating the days per month slept
dayspermonth = (hourspermonth) / 24
# telling the user how many days per month they sleep
print("you sleep for","{0:.2f}".format(dayspermonth),"days per month aswell!")
来自shell的响应: =========================重新启动:F:/ sleep hours.py ================ =========
Welcome to the sleep calculator, all you have to do is answer one question...
how many hours per night do you sleep?8
you sleep 56 hours per week
you also sleep 243.60 hours per month
you sleep for 10.15 days per month aswell!
答案 0 :(得分:1)
引起问题的两条线是: -
hourspermonth = print("you also sleep", hourspermonth,"hours per month")
和
dayspermonth = print("you sleep for",dayspermonth,"days per month aswell!")
调用print
并分配给变量不是有效的语法。删除作业,只需使用打印。这应该有用。
答案 1 :(得分:1)
仅限使用:
print("you also sleep", hourspermonth,"hours per month")
而不是:
hourspermonth = print("you also sleep", hourspermonth,"hours per month")
答案 2 :(得分:0)
你的代码是:
Traceback (most recent call last):
File "C:/Users/Leb/Desktop/Python/test.py", line 14, in <module>
dayspermonth = (hourspermonth) / (hourspernight) * 24
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
那是因为第12行放了hourspermonth = print("you also sleep", hourspermonth,"hours per month")
。从脚本中取出hourspermonth
。与最后一行相同。
#sleep calculator, user enters there hours slept over a nicght and the program
#will work out different facts about there sleaping
print("Welcome to the sleep calculator, all you have to do is answer one question...")
hourspernight = int(input("how many hours per night do you sleep?"))
#variable for the hours per week slept
hoursperweek = hourspernight * 7
#telling the user how many hours er week they sleep
print ("you sleep", hoursperweek,"hours per week")
#variable for how many hours per month they sleep
hourspermonth = float(hoursperweek * 4.35)
#teling the user how namy hours per month they sleep
print("you also sleep", hourspermonth,"hours per month")
#ISSUE, this is the variable that python has a problem with, and I'm not sure why
dayspermonth = (hourspermonth) / (hourspernight) * 24
#telling the user how many days per month they sleep (this bits ok... I think)
print("you sleep for",dayspermonth,"days per month aswell!")
旁注:在python中,如果你理解你的变量会更好。而不是使用hourspernight
使用hours_per_night
或更小hrs_night
,然后在其旁边添加评论,如果您需要。使其更具整体性。
修改强>
将第12行更改为:
print("you also sleep", "{0:.2f}".format(hourspermonth),"hours per month")
这将解决您的格式问题,并将其设为小数点后的2位数
修改2
将第14行和第16行更改为:
dayspermonth = (hourspermonth) / 24
...
print("you sleep for","{0:.2f}".format(dayspermonth),"days per month aswell!")
答案 3 :(得分:0)
因此,首先,当您打印输出时,您不想重新分配变量的值,因此只需打印即可。像这样:
# sleep calculator, user enters there hours slept over a nicght and the program
# will work out different facts about there sleaping
print("Welcome to the sleep calculator, all you have to do is answer one question...")
hourspernight = int(input("how many hours per night do you sleep?"))
# variable for the hours per week slept
hoursperweek = hourspernight * 7
# telling the user how many hours er week they sleep
print ("you sleep", hoursperweek,"hours per week")
# variable for how many hours per month they sleep
hourspermonth = float(hoursperweek * 4.35)
# teling the user how namy hours per month they sleep
print("you also sleep", hourspermonth,"hours per month")
# ISSUE, this is the variable that python has a problem with, and I'm not sure why
dayspermonth = (hourspermonth) / (hourspernight) * 24
# telling the user how many days per month they sleep (this bits ok... I think)
print("you sleep for",dayspermonth,"days per month as well!")
这是有效的。另外,在#
和评论开头之间留一个空格。