【问题标题】:I'm writing a fuel conversion program and its not working :(我正在编写一个燃料转换程序,但它不起作用:(
【发布时间】: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


【解决方案1】:

严重修剪并修复了压痕(我认为....)

if y is True 和类似的if y is not True 在这里毫无意义。

另外,说到is..is== 有时可以作为等效表达式来检查“相等”,但不一定。 == 检查是否相等,而 is 检查对象身份。您应该使用== 来检查两个对象之间的相等性。除了None,在这种情况下,通常首选使用is 而不是==

您在很多地方不必要地转换为str。它们已经是字符串了。

在您的 mpg 转换中,您已经有一个浮点数(可能是一个 int)。此处无需转换为 int。假设 mpg int 强制转换将使这个返回零

你的数学也倒退了。英里加仑。同样,公里加仑。

name = input("Hey, User, whats your name?   ")
print("Alright", name, "Welcome the the *gravynet* Fuel Efficiency Converter!")
mpg = input("Would you like to convert the fuel efficiency of your motor vehcile? (Miles Per Gallon) (y/n): ")

if mpg in y:

    miles = int(input("How far did you travel (in miles):   "))
    galls = int(input("How much fuel did you consume (in gallons):   "))

    mpgc = miles / galls

    print("The MPG Rate is:  ", mpgc)

else:
    kpl = input("Would you like to convert KPL instead? (y/n):   ")

    if kpl in y:
        kilometers = int(input("How far did you travel (in kilometers):   "))
        litres = int(input("How much fuel did you consume (in litres):   "))
        kplc = kilometers / litres
        print("The KPL Rate is:   ", kplc)

    else:

        print("No worries")
        print("Thanks", name, "for using *gravynet* Fuel Efficiency Coverter")
        print("Have a good day!")

【讨论】:

  • 非常感谢,我不知道“in”的用法,非常感谢您分享这些知识。非常感谢您的帮助
【解决方案2】:

python 中的is 关键字检查两个变量是否指向内存中的同一位置。 y 永远不会指向与单例 True 相同的内存位置,因为它的值是一个字符串。我怀疑你的意思是像

inp = str(input("Would you like to convert the fuel efficiency of your motor vehcile? (Miles Per Gallon) (y/n):    "))
if inp in y:
    ...

【讨论】:

  • 是的,Pythonista 在上面的评论中提到了同样的事情。也感谢您的意见;)
【解决方案3】:

你不能直接将y作为键盘按下,你必须将它作为输入(需要按回车),存储它,检查它是否满足条件,然后应用逻辑。

我看到您尝试将 y 和 n 定义为一个元组(有意或无意),在这种情况下,我假设您也想将这些其他词视为 yesor

在这种情况下,您可以应用此逻辑;

inp = input("Would you like to convert the fuel efficiency of your motor vehcile? (Miles Per Gallon) (y/n):    ")
if inp in y: # Check if inp corresponds any of the words defined in y
    # Things to do if `yes` or anything similar entered.

一些注意事项:

  • 如果您正在使用,则在输入后无需使用str() Python3(看起来你是)。因为input() 返回字符串。

  • 在某个地方你做了这样的事情:

    input(str("Would you like to convert KPL instead? (y/n): "))

    这更多余,因为您输入的值已经 一个字符串。

  • 在整个过程中,您也没有将某些输入分配给任何变量 代码。如果您以后要使用它们,您应该分配它们。

请注意这些问题。

【讨论】:

  • 感谢您的提示和技巧,我已经学会了使用 input = 123,如果 input in y then:.... 不过还是感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2021-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多