【发布时间】:2016-05-04 01:13:03
【问题描述】:
我是一个新手 Python 代码编写者,我从一个燃料转换程序开始。该程序会询问您的姓名,然后转换为英里/加仑或公里/升。目前,该程序运行良好,直到它到达转换为 MPG 行。然后一旦你按 y,它什么也不做。有趣的是,没有返回任何语法错误。请帮忙,因为我在上面找不到任何东西:(
import time
y = 'y', 'yes', 'yep', 'yea', 'ye'
n = 'n', 'no', 'nup', 'nay'
name = str(input("Hey, User, whats your name? "))
time.sleep(1.5)
print("Alright", name, "Welcome the the *gravynet* Fuel Efficiency Converter!")
time.sleep(1.5)
str(input("Would you like to convert the fuel efficiency of your motor vehcile? (Miles Per Gallon) (y/n): "))
if y is True:
miles = int(input("How far did you travel (in miles): "))
galls = int(input("How much fuel did you consume (in gallons): "))
mpgc = (galls/miles)
print("The MPG Rate is: ", int(mpgc))
time.sleep(2)
print("test print")
if y is (not True):
input(str("Would you like to convert KPL instead? (y/n): "))
time.sleep(1.5)
if y is True:
kilometers = int(input("How far did you travel (in kilometers): "))
litres = int(input("How much fuel did you consume (in litres): "))
kplc = ( litres / kilometers )
print("The KPL Rate is: ", int(kplc))
time.sleep(3)
exit()
if y is not True:
print("No worries")
time.sleep(1.5)
print("Thanks", name, "for using *gravynet* Fuel Efficiency Coverter")
time.sleep(1.5)
print("Have a good day!")
time.sleep(1.5)
exit()
else :
print("Sorry, invalid response. Try again")
exit()
elif not y:
print("Please use y/n to answer" )
time.sleep(2)
elif not n:
print("Please use y/n to answer" )
time.sleep(2)
对不起,如果您认为这很糟糕,但我刚开始使用 python,我需要一些帮助 :)
【问题讨论】:
-
好吧,一方面,
if y is True毫无意义。它永远是错误的。y是一个常量元组。这不是字面意义上的True。 -
顺便说一下,在 python 中,
is意味着不同的东西。现在只需使用==。
标签: python