Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建变量依赖于另一个变量_Python_Variables - Fatal编程技术网

Python 创建变量依赖于另一个变量

Python 创建变量依赖于另一个变量,python,variables,Python,Variables,几天前我开始学习python,我想创建一个简单的程序,创建一个总结文本,显示我买了什么,我付了多少钱,这取决于我的输入。到目前为止,我已经创建了这个程序,从技术上讲,它运行良好。但是我在指定文本中可能依赖于我的输入的部分时遇到了问题。 代码: 因此,当输入以1结束时,问题就开始了。例如21、31等等,除了11作为结论文本会说“你买了41个苹果和21个香蕉……”。甚至可以将“apple/s”、“banana/s”、“dollar/s”作为依赖于输入变量的变量吗 从哪里开始创建依赖于输入变量的变量

几天前我开始学习python,我想创建一个简单的程序,创建一个总结文本,显示我买了什么,我付了多少钱,这取决于我的输入。到目前为止,我已经创建了这个程序,从技术上讲,它运行良好。但是我在指定文本中可能依赖于我的输入的部分时遇到了问题。 代码:

因此,当输入以1结束时,问题就开始了。例如21、31等等,除了11作为结论文本会说“你买了41个苹果和21个香蕉……”。甚至可以将“apple/s”、“banana/s”、“dollar/s”作为依赖于输入变量的变量吗

  • 从哪里开始创建依赖于输入变量的变量
  • 如何通过数字结尾定义“香蕉”或“香蕉”的标准?同时也将11排除在标准之外,因为这也是“香蕉”,但以1结尾

  • 看起来这可能是一项简单的任务,但我仍然无法理解这一点,因为我最近才开始学习python。我尝试为变量创建IF和Dictionary,但失败。

    看起来您可能对它感兴趣

    例如:

    def pluralize(x):
        for i in ["a", "e", "i", "o", "u"]:
            # Is vowel
            if x.endswith(i):
                return x+"s"
        # Is consonant
        return x
    
    print(pluralize("Hello World"))
    
    注意:您可能希望改进此函数以处理以y结尾、以x或s结尾等情况

    编辑:


    您需要一个if语句来检查指定的输入是否大于或等于1。这很简单,需要一个
    if
    语句

    一种简单的方法是:

    if apples == '1':
        strApples = str(apples, 'apple')
    else:
        strApples = str(apples, 'apples')
    
    …等等所有其他水果

    然后,您可以打印您的结论:

    print("You bought", strApples, "and", strBananas + ". You paid " +dollars +" dollars.")
    
    但这不是最好的展示方式。 我会逐行得出结论:

    print("You bought: ")
    
    if apples == '1':
        print("1 apple")
    else:
        print(apples, "apples")
    
    if bananas == '1':
        print("1 banana")
    else:
        print(bananas, "bananas") 
    
    print("You paid", dollars, "dollars")
    
    编辑:

    我想我现在明白了,您希望每个以“1”结尾的数字,而不是以“11”为单数显示

    这可以通过以下方式完成:

    apples = input("How many apples did you buy?: ")
    bananas = input("How many bananas did you buy?: ")
    dollars = input("How much did you pay: ")
    
    print("You bought: ")
    
    if int(apples[-1]) == 1 and int(apples) != 11:
        print(apples, "apple")
    else:
        print(apples, "apples")
    
    if int(bananas[-1]) == 1 and int(bananas) != 11:
        print(bananas, "banana")
    else:
        print(bananas, "bananas")
    
    print("You paid", dollars, "dollars")
    

    “41个苹果”有什么问题?你要求的是,如果你的单数名词不止一个,就把它们变成复数。你必须自己做这种多元化,还是可以使用图书馆?回答有帮助吗?如果你感兴趣,我可以做一个更深入的多元函数。那将是非常感谢。到目前为止,这个问题只在只有一个苹果或香蕉的情况下才得以解决,而不是只有21个或31个。。。我意识到这可能是针对我的母语的,它要求每个以1结尾的数字(11除外)都使用单数。我建议您通读所有代码,您可能会学到一些东西。谢谢,当我添加更多变量时,这肯定会很有用。非常欢迎您。我想我将来的项目也会用到这个。是的,很抱歉我的误解。很明显,我并没有意识到英语和我的母语之间有这么小的差别。一点问题也没有。不知何故,我仍然坚持着这个观点。在这种情况下也可以使用.endswith吗?例如:
    if apples.endswith(1):print(apples,“apple”)else:print(apples,“apples”)
    是的,实际上应该可以。但是,
    .endswith()
    需要传递一个字符串才能使其工作,因此如果apples.endswith('1'):等。。。
    apples = input("How many apples did you buy?: ")
    bananas = input("How many bananas did you buy?: ")
    dollars = input("How much did you pay: ")
    
    print("You bought: ")
    
    if int(apples[-1]) == 1 and int(apples) != 11:
        print(apples, "apple")
    else:
        print(apples, "apples")
    
    if int(bananas[-1]) == 1 and int(bananas) != 11:
        print(bananas, "banana")
    else:
        print(bananas, "bananas")
    
    print("You paid", dollars, "dollars")