简体   繁体   English

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

[英]Creating variable depended on another variable

I started learning python few days ago and im looking to create a simple program that creates conclusion text that shows what have i bought and how much have i paid depended on my inputs.几天前我开始学习 python 并且我希望创建一个简单的程序来创建结论文本,显示我买了什么以及我支付了多少取决于我的输入。 So far i have created the program and technically it works well.到目前为止,我已经创建了该程序,并且从技术上讲它运行良好。 But im having a problem with specifying the parts in text that might depend on my input.但是我在指定可能取决于我的输入的文本部分时遇到问题。 Code:代码:

apples = input("How many apples did you buy?: ")
bananas = input("How many bananas did you buy?: ")
dollars = input("How much did you pay: ")
print("")
print("You bought " +apples+ " apples and " +bananas+ " bananas. You paid " +dollars +" dollars.")
print("")
print("")
input("Press ENTER to exit")
input()

So the problem begins when input ends with 1. For example 21, 31 and so on, except 11 as the conclusion text will say "You bought 41 apples and 21 bananas...".所以当输入以 1 结尾时问题就开始了。例如 21、31 等等,除了 11 作为结论文本会说“你买了 41 个苹果和 21 个香蕉......”。 Is it even possible to make "apple/s", "banana/s", "dollar/s" as variables that depend on input variables?甚至可以将“apple/s”、“banana/s”、“dollar/s”作为依赖于输入变量的变量?

  1. Where do i start with creating variable that depends on input variable?我从哪里开始创建取决于输入变量的变量?
  2. How do i define the criteria for "banana" or "bananas" by the ending of number?我如何通过数字结尾来定义“香蕉”或“香蕉”的标准? And also exclude 11 from criteria as that would be also "bananas" but ends with 1.并且还从标准中排除 11,因为这也是“香蕉”,但以 1 结尾。

It seems like this could be an easy task but i still can't get my head around this as i only recently started learning python.看起来这可能是一项简单的任务,但我仍然无法解决这个问题,因为我最近才开始学习 Python。 I tried creating IF and Dictionary for variables but failed.我尝试为变量创建 IF 和 Dictionary 但失败了。

Looks like you might be interested by functions .看起来您可能对functions感兴趣。

For example:例如:

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"))

Note: you may want to improve this function to handle things like ending with y, ending with x or s and so on.注意:您可能希望改进此函数以处理诸如以 y 结尾、以 x 或 s 结尾等问题。

EDIT:编辑:

https://pastebin.com/m2xrUWx9 https://pastebin.com/m2xrUWx9

You would want to have an if statement that checks whether the specified input falls above or equal to 1. This is simple to do and requires an if statement.您可能需要一个 if 语句来检查指定的输入是否大于或等于 1。这很简单,并且需要一个if语句。

A simple way of doing this would be:一个简单的方法是:

if apples == '1':
    strApples = str(apples, 'apple')
else:
    strApples = str(apples, 'apples')

...and so forth for all the other fruits. ...等等所有其他水果。

You would then print your conclusion with:然后,您将使用以下内容打印您的结论:

print("You bought", strApples, "and", strBananas + ". You paid " +dollars +" dollars.")

Not the best way of displaying this though.不过,这不是展示这一点的最佳方式。 I would go for a line by line conclusion:我会逐行得出结论:

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")

EDIT:编辑:

I think I now understand that you want every number ending in '1', that is not '11' to be displayed as singular.我想我现在明白你希望每个以“1”结尾的数字,而不是“11”显示为单数。

This can be done using the following:这可以使用以下方法完成:

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM