Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How would I define a function for this? [Noob question]

I´m working on creating a list for a game I play with my friends where words get added x times to that list.
Currently, I´m using the same three lines of code 5 times and I´d like to instead just call up a predefined funtion 5 times. is that possible? (I only translated the first line)

AmountApple = int(input("How many apples?"))
for num in range(AmountApple):
    newList.append("apple")
    
AnzahlW2 = int(input("Wie viele Werwolf2?"))
for num in range(AnzahlW2):
    Rollen.append("Werwolf2")
    
AnzahlA = int(input("Wie viele Amor?"))
for num in range(AnzahlA):
    Rollen.append("Amor")
    
AnzahlHexe = int(input("Wie viele Hexe?"))
for num in range(AnzahlHexe):
    Rollen.append("Hexe")
    
AnzahlHure = int(input("Wie viele Hure"))
for num in range(AnzahlHure):
    Rollen.append("Hure")

AnzahlB = int(input("Wie viele Bürger"))
for num in range(AnzahlB):
    Rollen.append("Bürger")

Any help or pointing out where I mightve gone wrong would be appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A bigger sample of your script maybe would have helped, but from my understanding of your script if I had this problem I would do this:

def fruitFunction(fruit, newList):
    amountFruit = int(input(f"How many {fruit}?"))
    for amount in range(amountFruit):
        newList.append(fruit)

    return newList

You can see that I used f-strings here. f-strings allow you to put variables in strings without having to use + or , and have lots of double-quotes. I recommend that you use f-strings everywhere you can.

There is a problem though, and that’s the fact that you need to know the singular and plural of the fruit. But it can be solved like this:

def fruitFunction(singularFruit, pluralFruit, newList):
    amountFruit = int(input(f"How many {pluralFruit}?"))
    for amount in range(amountFruit):
        newList.append(singularFruit)

    return newList

To use this function you just have to do this: newList = fruitFunction('apple', 'apples', newList)
Keep in mind that you have to have a list already made to call this function.

Also, it might be a good idea to instead of adding a fruits name 5 times for example in the list, you can append a list to the list. The list would contain the name of the fruit and the amount. It can be done like this:

amountFruit = int(input(f"How many {fruit}?"))
newList.append([fruit, amountFruit])

And that should work, thanks.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading