【问题标题】:Python Store VariablesPython 存储变量
【发布时间】:2012-11-24 03:43:00
【问题描述】:

我想将整数存储为 Rate、Remaining、Battery 和 kWh。请帮助修改它,以便我可以存储变量并在代码底部执行方程式。

Rate = 0
while (Rate < 0.5) or (Rate > 2.0):
    Rate = int(raw_input("Enter Desired Rate of Charge: ")) #User will be prompted to enter the charge rate of the system
    if Rate < 0.5:
    #If the charge rate entered is less than 0.5 kWhs
        print "Charge Rate Too Low: Please consider revision" #Tells the user the rate is too low
    elif Rate > 2.0:
    #Also, if the charge rate entered is greater than 2.0 kWhs...
        print "Charge Rate Too High: Please consider revision" #Tells the user the rate is too high
    else:
    #Otherwise, for any number entered between the bounds of 0.5 and 2.0 kWhs...
        print '\n' #Page break for new conditions.

import random
Remaining = random.randint(0, 80) #Simulates remaining charge left over from the last use of the car. Defines the variable as "Remaining"

Battery = 0
while (Battery <=0) or (Battery >=80):
    Battery = int(raw_input("Enter Current Battery Life: "))
    #User will be prompted to enter the current battery level of the car. Defines that variable as "Battery"
    if Battery < 0:
    #If user input level is less than 0 kWh...
        print "Insufficient Entry (Too Low): Please consider revision" # Tells the user that the battery level entered is too low
    elif Battery > 80:
    #Also, if the user input level is greater than 80 kWh...
        print "Insufficient Entry (Too High): Please consider revision" # Tells the user that the battery level entered is too high
    kWh = 80 - Battery - Remaining
    print '\n' #Page break
    if kWh <=0:
        print "Battery Fully Charged!"
        print "There were " Remaining, " kWhs remaing from the last charge. 0 kWhs were charged."
        print "Buckle up! The rewards of the journey far outweigh the risk of leaving the garage!"
    else:
        print "Battery Fully Charged!"
        print "There were " Remaining, " kWhs remaing from the last charge. " kWh, "kWhs were added to recharge battery."
        print "Buckle up! The rewards of the journey far outweigh the risk of leaving the garage!"

【问题讨论】:

  • 这与您的问题几乎无关,而只是一般性评论:您不需要编写对于阅读代码的人来说完全多余的 cmets。仅评论以解释不明显的事情。例如,您不需要在进行&lt; 0 检查之后说“如果用户级别小于 0”,因为它们的含义完全相同。相反,您可能想说为什么零是应该测试的边界。对于像print 这样的声明,很少需要评论,因为打印的文本是它自己的文档。
  • 充电率不以千瓦时表示。 kWh 是储存的能量。速率是一个数量除以时间,所以你得到的是 (k)W 作为速率。顺便说一句,Rate 在哪里使用?
  • 另一件事:根据PEP 8,你应该用小写写你的变量名:batteryrateremaining。并考虑将kWh 重命名为added 或类似名称:几乎所有值都以千瓦时为单位...

标签: python variables store


【解决方案1】:

如果您告诉我们问题出在哪里(完全追溯错误),我们本可以立即帮助您并告诉您

print "There were " Remaining, " kWhs remaing from the last charge. 0 kWhs were charged."
print "There were " Remaining, " kWhs remaing from the last charge. " kWh, "kWhs were added to recharge battery."

是问题所在:您在RemainingkWh 之前缺少,

使用

print "There were", Remaining, "kWhs remaing from the last charge. 0 kWhs were charged."
print "There were", Remaining, "kWhs remaing from the last charge. ", kWh, "kWhs were added to recharge battery."

一切都会好起来的。

如果您遇到类似的错误,请查看那里发生的情况并纠正它,直到它运行为止。

顺便说一句:您重复了代码。最好这样做

print "Battery Fully Charged!"
print "There were", Remaining, "kWhs remaing from the last charge. ", # notice the ,
if kWh <=0:
    print "0 kWhs were charged."
else:
    print "kWh, "kWhs were added to recharge battery."
print "Buckle up! The rewards of the journey far outweigh the risk of leaving the garage!"

(我不确定是否最好只写kWh 而不是kWhs。单位缩写有复数形式吗?我不这么认为......)

【讨论】:

    【解决方案2】:

    替换

    else:
        while Battery >=80:
            kWh=80-'Battery'-'Remaining'
            print kWh, "kWh"
    

    简单的

    kWh = 80 - Battery - Remaining
    print(kWh, "kWh")
    

    【讨论】:

    • 非常感谢!还有一件事,我以后如何回忆变量“Remaining”?
    • 它已经存储在变量绑定Remaining中。只需在您想引用它的任何地方引用Remaining。还是我误解了您的真正要求?
    • 我编辑了这个问题。它一直告诉我有一个语法错误。
    • 应该类似于print "string" + str(variable) + "more string"。您必须做一些事情才能将变量转换为字符串(str() 函数执行此操作)并且您必须将字符串片段连接在一起(+ 执行该部分)
    猜你喜欢
    • 2020-09-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多