Creating variable depended on another variable

Viewed 59

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. 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...". Is it even possible to make "apple/s", "banana/s", "dollar/s" as variables that depend on input variables?

  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.

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. I tried creating IF and Dictionary for variables but failed.

2 Answers

Looks like you might be interested by 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.

EDIT:

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.

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.

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