【问题标题】:ValueError: invalid literal for int() with base 10 cannot figure out whyValueError:以 10 为底的 int() 的无效文字无法弄清楚原因
【发布时间】:2014-10-31 23:28:30
【问题描述】:

我似乎无法弄清楚为什么当我运行我的程序时会收到错误 ValueError: invalid literal for int() with base 10: 'Enter pennies : '。 整个程序是由我的导师制作的,因此我们可以添加功能以使其正常工作。我目前正在尝试定义 get_input1 但我没有运气。任何帮助都会很棒。

def main():
    pennies = get_input1("Enter pennies : ")
    nickels = get_input("Enter nickels : ")
    dimes = get_input("Enter dimes : ")
    quarters = get_input("Enter quarters : ")

    print("You entered : ")
    print("\tPennies  : " , pennies)
    print("\tNickels  : " , nickels)
    print("\tDimes    : " , dimes)
    print("\tQuarters : " , quarters)


    total_value = get_total(pennies, nickels, dimes, quarters)
    dollars = get_dollars(pennies, nickels, dimes, quarters)
    left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)

    print("Total = $", total_value, sep="")
    print("You have", dollars, "dollars and", left_over_cents, "cent(s)")

def get_input1(pennies):
    int(input("Enter Pennies: "))
    if int(pennies) < 0:
        print('Error: money cannot be negative')
        pennies = int(input('Enter correct amount of pennies: '))



main()

【问题讨论】:

    标签: python


    【解决方案1】:

    改变这个:

    int(input("Enter Pennies: "))
    

    到这里:

    pennies = input("Enter Pennies: ")
    

    已编辑 我相信这只是一个错字,您应该为输入结果分配便士。

    【讨论】:

    • OP 没有为名称 int 分配任何东西,他们不应该做的是将字符串传递给他们从不使用的函数,get_input 应该不带参数
    • @PadraicCunningham,谢谢我编辑了我的答案,我也相信这只是一个真正的错误,忘记将“便士”分配给输入结果,而不是不理解它必须这样做。
    【解决方案2】:
    def main():
        pennies = get_input1("Enter pennies : ")
        nickels = get_input2("Enter nickels : ")
        dimes = get_input3("Enter dimes : ")
        quarters = get_input4("Enter quarters : ")
    
        print("You entered : ")
        print("\tPennies  : " , pennies)
        print("\tNickels  : " , nickels)
        print("\tDimes    : " , dimes)
        print("\tQuarters : " , quarters)
    
    
        total_value = get_total(pennies, nickels, dimes, quarters)
        dollars = get_dollars(pennies, nickels, dimes, quarters)
        left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
    
        print("Total = $", total_value, sep="")
        print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
    
    def get_input1(pennies):
        pennies = int(input("Enter Pennies: "))
        if int(pennies) < 0:
            print('Error: money cannot be negative')
            pennies = int(input('Enter correct amount of pennies: '))
    
        return(pennies)
    
    def get_input2(nickels):
        nickels = int(input("Enter nickels: "))
        if int(nickels) < 0:
            print('Error: money cannot be negative')
            pennies = int(input('Enter correct amount of nickels: '))
    
        return(nickels)
    
    def get_input3(dimes):
        dimes = int(input("Enter dimes: "))
        if int(dimes) < 0:
            print('Error: money cannot be negative')
            pennies = int(input('Enter correct amount of dimes: '))
    
        return(dimes)
    
    def get_input4(quarters):
        quarters = int(input("Enter quarters: "))
        if int(quarters) < 0:
            print('Error: money cannot be negative')
            pennies = int(input('Enter correct amount of quarters: '))
    
        return(quarters)
    
    main()
    

    这将帮助您定义您的便士、镍币、角钱和硬币。现在您必须定义 get_total、get_dollar 和 get_left_over_cents。

    您需要在 int 值前面加上便士,这就是您收到错误的原因。然后你可以将硬币的价值返回给 main。

    希望这会有所帮助!

    【讨论】:

    • 否决这个,因为这个网站不是为某人的编程任务做一半的工作。但也许我错了。
    【解决方案3】:

    您的数据流是反向的。在 main() 中,您调用 get_input1 来检索值,这与 input() 非常相似,但如果我们查看 get_input1 的作用:

    # Names its argument, which was a question, "pennies"
    def get_input1(pennies):
        # Asks a question, converts to int, and discards it
        int(input("Enter Pennies: "))
        if int(pennies) < 0:    # Converts pennies to int; but it was a question!
            print('Error: money cannot be negative')
            # This is the only place to alter pennies, but it doesn't return it
            pennies = int(input('Enter correct amount of pennies: '))
    

    看起来这曾经是被重构以处理各种硬币的代码,每个硬币都有一个函数调用,但它没有它需要的两个修改:一种改变它要求的方法,以及一种方法返回答案。请注意,函数参数,如 get_input1 中的便士,是局部变量; main 永远不会看到它改变。

    【讨论】:

      猜你喜欢
      • 2018-09-09
      • 2020-01-04
      • 2010-12-22
      • 2011-07-07
      • 2019-10-14
      • 2022-05-19
      相关资源
      最近更新 更多