2

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.

Gints
  • 23
  • 3
  • 1
    What is wrong with ‘41 apples’? – quamrana Nov 24 '20 at 21:46
  • 1
    What you're asking for is to make all your singular nouns **plural** if there's more than one of them. Do you have to do this pluralization yourself, or can you use a library? Does [this](https://stackoverflow.com/questions/18902608/generating-the-plural-form-of-a-noun) answer help? – Random Davis Nov 24 '20 at 21:49

2 Answers2

1

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

ericl16384
  • 326
  • 3
  • 12
  • If you are interested, I could make a more in-depth pluralization function. – ericl16384 Nov 24 '20 at 21:54
  • That would be highly appreciated. So far problem is solved only in situations when there is only 1 apple or banana but it not with 21 or 31... I realised this might be specific for my native language that it requires every number that ends with 1 (except 11) to be used singular – Gints Nov 24 '20 at 22:11
  • 1
    https://pastebin.com/m2xrUWx9 I recommend you look through all of the code, you might learn something. – ericl16384 Nov 24 '20 at 22:49
  • 1
    Thanks this definitely will be useful when i will add more variables. – Gints Nov 24 '20 at 22:58
  • You are quite welcome. I think I will use this for my future projects also. – ericl16384 Nov 24 '20 at 23:08
1

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")
Miles B
  • 79
  • 6
  • 1
    Yeah, sorry for the misunderstanding on my part. Clearly i was not aware of this small difference in english and my native language. – Gints Nov 24 '20 at 22:32
  • 1
    No problem at all – Miles B Nov 24 '20 at 22:52
  • 1
    Somehow im still stuck at this. Would it be possible to use .endswith in this situation aswell? For example: `if apples.endswith(1): print(apples, "apple") else: print(apples, "apples")` – Gints Nov 25 '20 at 19:02
  • 1
    Yes actually, that should work. However `.endswith()` needs to be passed a string for it to work, so you would do `if apples.endswith('1'):` etc... – Miles B Nov 26 '20 at 17:55