首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据我的程序中用户的输入给出一个错误信息?

如何根据我的程序中用户的输入给出一个错误信息?
EN

Stack Overflow用户
提问于 2016-08-04 21:43:06
回答 4查看 83关注 0票数 3

所以我用Python写了一个电动比尔计算器。好消息是它起作用了!然而,我需要使它更好地工作。当被问到每天有多少小时时,用户可以输入25个小时,或者输入他们想要的任何其他数字。很明显一天不会有25个小时。我试过几件事,但我不能让它正常工作。

我试过这样的方法:

代码语言:javascript
运行
复制
hours = input("How many hours per day? ")
if hours > 24:
    print("Don't be silly, theres not more than 24 hours in a day ")
else:
    main()

我要做的是,让程序显示一个错误信息,如果输入超过24小时,如果输入超过24小时,那么跳到下面的代码,询问他们是否想尝试另一种计算。如果他们进入24小时或更短的时间,然后继续程序,没有错误信息。我花了大约两天的时间来修复这个问题,在谷歌搜索了几个小时,我可能已经找到了正确的答案,但似乎无法让它发挥作用。我想我需要一些时间真实的陈述,或者如果;那么,但是,尽管我已经试过很多次,我在这一点上还是在拔出我的头发。整个程序的代码如下:

代码语言:javascript
运行
复制
def main():
    star = '*' * 70
    print star
    print ("Nick's Electric Bill Calculator")
    print star
    watts = input("Enter the watts of appliance, or total watts of        appliances ")
    hours = input("Enter the number of hours that appliance(s) run per day ")
    cost = input("Enter the cost in cents per KwH you pay for electricty " )

    # print ("Don't be silly, there isn't more than 24 hours in a day!")

    x = (watts * hours / 1000.0 * 30) * cost
    total = x
    print star
    print ("""If you use %s watts of electricity for %s hours per day, at a cost of
%s cents per Kilo-watt hour, you will add $%s to your monthly
electric bill""") % (watts, hours, cost, total)
    print star

playagain = 'yes'
while playagain == 'yes':
    main()
    print('Would you like to do another Calculation? (yes or no)')
    playagain = raw_input()

我刚开始编程,我只学了几个星期的Python,非常感谢你的建议。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-08-04 21:56:06

@JamesRusso代码的优化版本:

代码语言:javascript
运行
复制
def main():
    star = '*' * 70
    print star
    print ("Nick's Electric Bill Calculator")
    print star
    watts = int(input("Enter the watts of appliance, or total watts of appliances"))
    hours = int(input("Enter the number of hours that appliance(s) run per day"))
    # Check that the hours number is good before getting to the cost
    while (hours > 24) or (hours < 0):
        print("Don't be silly, theres not more than 24 or less than 0 hours in a day ")
        hours = int(input("Enter the number of hours that appliance(s) run per day "))
    cost = int(input("Enter the cost in cents per KwH you pay for electricty "))

    # We don't need an auxiliary variable x here
    total = (watts * hours / 1000.0 * 30) * cost

    print star

    print ("""If you use %s watts of electricity for %s hours per day, at a cost of %s cents per Kilo-watt hour, you will add $%s to your monthly electric bill""") % (watts, hours, cost, total)
       print star

if __name__ == '__main__':
    playagain = 'yes'
    while playagain == 'yes': 
        main()
        print 'Would you like to do another Calculation? (yes or no)'
        playagain = raw_input()
        # Checking that the user's input is whether yes or no
        while playagain not in ['yes', 'no']:
            print "It's a yes/no question god damn it"
            print 'Would you like to do another Calculation? (yes or no)'
            playagain = raw_input()
票数 0
EN

Stack Overflow用户

发布于 2016-08-04 21:46:01

我将您的错误消息放在if状态中,计算代码放在其他状态中。这样,当时间不正确时,它就不会进行计算,而是退出main,回到while循环,询问用户是否愿意再次播放。另外,正如其他用户所指出的,按照我下面所做的,您应该添加更多的错误测试和负数信息,等等。

代码语言:javascript
运行
复制
def main():
    star = '*' * 70
    print star
    print ("Nick's Electric Bill Calculator")
    print star
    watts = input("Enter the watts of appliance, or total watts of        appliances ")
    hours = input("Enter the number of hours that appliance(s) run per day ")
    cost = input("Enter the cost in cents per KwH you pay for electricty " )

    if hours > 24:
        print("Don't be silly, theres not more than 24 hours in a day ")
    else:
        x = (watts * hours / 1000.0 * 30) * cost
        total = x
        print star
        print ("""If you use %s watts of electricity for %s hours per day, at a cost of %s cents per Kilo-watt hour, you will add $%s to your monthly electric bill""") % (watts, hours, cost, total)
       print star

playagain = 'yes'
while playagain == 'yes': 
main()
print('Would you like to do another Calculation? (yes or no)')
playagain = raw_input()
票数 0
EN

Stack Overflow用户

发布于 2016-08-04 21:47:27

您从用户获得的输入是string类型,但您可以将其与int类型进行比较。这是个错误。您应该首先将string转换为int

代码语言:javascript
运行
复制
hours = int(input("How many hours per day? "))
#    ----^---

然后你就可以做这样的事情:

代码语言:javascript
运行
复制
while hours > 24:
    print("Don't be silly, theres not more than 24 hours in a day ")
    hours = int(input("How many hours per day? "))

如果您不确定用户是否会输入一个数字,则应该使用try/catch状态器。

除此之外,您应该正确地验证代码缩进。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38777883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档