【问题标题】:Python 3.4 code help - I can't make this work [closed]Python 3.4 代码帮助 - 我无法完成这项工作 [关闭]
【发布时间】:2015-03-31 14:33:39
【问题描述】:

我是 python 新手。这是我的代码:

print("Welcome to the Currency Converter")

print("This Currency Converter converts: ")

print(" US [D]ollar")

print(" [E]uro")

print(" British[P]ound Sterling")

print(" Japanese[Y]en")

print()

def input1() :

    a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
    if a.lower is not ('e','y','p','d'):
        print("That is not a valid currency inital. Please try again ")
        input1()
input1()

def input2() :

    b = input("Enter the currency inital you wish to convert to: ")
    if b.lower is not ('e','y','p','d'):
        print("That is not a valid currency inital. Please try again")
        input2()
input2()

它总是在重复,即使我不想这样。我的目标是仅在输入e,y,dp 时使其工作,否则应显示错误消息并重复问题。

【问题讨论】:

  • 你可能想要not in (...)
  • 请阅读while loops.上的Python文档
  • a.lower 是一个函数,你必须调用它:a.lower(),而你缺少一个 in 关键字。
  • 我真的很怀念“缺乏最低限度的理解”的关闭原因:/
  • @vaultah 确实有比这更糟糕的问题...

标签: python function python-3.x converter currency


【解决方案1】:
  1. a.lower 是一个你需要调用的函数,否则你只会得到一个函数引用。 a.lower()也是如此。
  2. if x is not ('e', 'y', 'p', 'd')is(或is not)运算符检查身份。左侧是用户输入,右侧是一个包含可能字符的 4 元素元组。这两个永远不会有相同的身份。您想使用in 运算符:

    if a.lower() not in ('e', 'y', 'p', 'd'):
        …
    

【讨论】:

    【解决方案2】:

    您的代码没有正确调用函数,也没有为转换分配全局变量。此外,您不应使用检查内存中等效引用的is 关键字,而应使用检查元组中是否存在元素的in 关键字。

    print("Welcome to the Currency Converter")
    
    print("This Currency Converter converts: ")
    
    print(" US [D]ollar")
    
    print(" [E]uro")
    
    print(" British[P]ound Sterling")
    
    print(" Japanese[Y]en")
    
    print()
    
    def input1() :
    
        a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
        if a.lower() not in ('e','y','p','d'):
            print("That is not a valid currency inital. Please try again ")
            return input1()
        return a
    
    a = input1()
    
    def input2() :
    
        b = input("Enter the currency inital you wish to convert to: ")
        if b.lower() not in ('e','y','p','d'):
            print("That is not a valid currency inital. Please try again")
            return input2()
        return b
    
    b = input2()
    

    【讨论】:

    • 是的,抱歉,我忘了修复in
    • 现在有什么问题?
    • 第一次输入错误的时候,有没有检查函数的返回值?
    • 这个 Jasper 怎么样,递归?
    • 更好 :-) 我还是宁愿在这里使用循环而不是递归,但这只是个人喜好问题
    【解决方案3】:

    有几个问题:

    您正在读取函数中的输入,但从未返回它。

    def fun():
      a = 1
    
    fun()
    print(a)  # NameError
    

    您的测试失败,因为您将方法 (a.lower) 与元组进行比较。您必须调用该函数并检查结果是否为in 序列类型:

    if a.lower() not in ('e', 'y', 'p', 'd'):
      ...
    

    终于不用递归调用input1函数了:

    def input1() :
      while True:    
        a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
        if a.lower() not in ('e','y','p','d'):
          print("That is not a valid currency inital. Please try again ")
        else:
          return a.lower()
    

    然后在脚本的“主要”部分:

    from_curr = input1()
    

    (不要使用from作为变量名,因为它是keyword)。

    【讨论】:

      猜你喜欢
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      相关资源
      最近更新 更多