So, for practice, I attempted to make this sorry program to try to tell you how many beers you could have based on how much money you wanna spend and what you want to drink. I end up getting all tangled up in my own ignorance and I was wondering what the best way to go about something like that is.
Thanks in advance I am trying to force myself to learn by giving myself practice that i can laugh about when my friends use it.
#how many beers can you afford?
n = input("State name: ")
print("oh hey whats up " + n)
#useless line of code i know
beer = ["GoodPeople","Budweiser","fourloko","NattyIce"]
#wanted to give a list of the beers available
print(beer)
d = input("pick your poison")
c = input("How Much money you wanna spend tonight?")
GoodPeople = (/5)
Budweisor = (/2)
fourloko = (/3)
Nattyice = (/2)
if d = GoodPeople
print("you can afford" c/d)
#this is where i mess up because as i want them to input which beer they want i also want to calculate how much money they have © divided by the price of the beer (d)
Nov 28, 2016 05:36 AM
(This post was last modified: Nov 28, 2016 05:37 AM by DarkDelta.)
PHP Code
<?php # Made by DarkDelta # Have fun with it
name = input("Enter your name: ") print("Hello {}! Welcome to the Beer Calculator!".format(name))
# Now the real work begins. # We'll start with defining what we need..
# A list of tuples, containing the data we need beer = [ ("Budweiser", 2), ("FourLoko", 3), ("GoodPeople", 5), ("NattyIce", 2) ] # Show the available beers print("We have the following in stock for you:")
# We want a tuple to use, so we define that here. for (beer_name, divide) in beer: # Enumerating through all items in the list of tuples print("-> {}".format(beer_name)) # And showing the beer name, use .format() when working with variable data in strings.
# Let the user choose a beer beer_choice = input("Choose your poison: ") # And we want the budget, make sure to wrap integer inputs in an int() budget = int(input("Amount to spend: "))
# Here the fun begins, again iterate through the list for (beer_name, divide) in beer: # Check if the currently enumerated item is the item the user chose if beer_name.lower() == beer_choice.lower(): # The enumerated item is what the user chose # Now we divide the budget by the division amount amount_of_beers = budget / divide # And we tell the user what he wants to know print("You can afford {} beers from {}".format(amount_of_beers, beer_name)) # The program ends here
The code has comments explaining every step
1DarkDTkHTm5ouTPrZ6bUHpH345x4uiAfV
use a dictionary instead of a list {beer1:price1,beer2:price2,...}
Thanks for the quick help guys, much appreciated.
(Nov 28, 2016 05:36 AM)DarkDelta Wrote: PHP Code
<?php # Made by DarkDelta # Have fun with it
name = input("Enter your name: ") print("Hello {}! Welcome to the Beer Calculator!".format(name))
# Now the real work begins. # We'll start with defining what we need..
# A list of tuples, containing the data we need beer = [ ("Budweiser", 2), ("FourLoko", 3), ("GoodPeople", 5), ("NattyIce", 2) ] # Show the available beers print("We have the following in stock for you:")
# We want a tuple to use, so we define that here. for (beer_name, divide) in beer: # Enumerating through all items in the list of tuples print("-> {}".format(beer_name)) # And showing the beer name, use .format() when working with variable data in strings.
# Let the user choose a beer beer_choice = input("Choose your poison: ") # And we want the budget, make sure to wrap integer inputs in an int() budget = int(input("Amount to spend: "))
# Here the fun begins, again iterate through the list for (beer_name, divide) in beer: # Check if the currently enumerated item is the item the user chose if beer_name.lower() == beer_choice.lower(): # The enumerated item is what the user chose # Now we divide the budget by the division amount amount_of_beers = budget / divide # And we tell the user what he wants to know print("You can afford {} beers from {}".format(amount_of_beers, beer_name)) # The program ends here
The code has comments explaining every step
Not 100% what I would do, but I support this solution.
(Nov 29, 2016 11:53 AM)Goodies Wrote: Not 100% what I would do, but I support this solution.
Seemed as close as I could get to his original code without changing much of the already applied logic by OP.
1DarkDTkHTm5ouTPrZ6bUHpH345x4uiAfV
|