Python Forum

Full Version: Simple Text Shop Simulator Feedback
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all, I am very new to coding as in I have only 7 hours of research and experience under my belt. I chose to pick up Python and it's been a blast. I've created a small text shop simulator from scratch and would like to have some feedback on my first project. It currently stops after you purchase something and I don't think I will continue any further unless I can start making the code more complex.

I want to start heading towards GUI coding and getting different displays for my code, but I have no clue where to start learning about how to do so. Any suggestions? Or any suggestions on what to add to this code or what to do next in my coding adventure?

# requirement
correct_country = "Australia"

# contact details
name = input("Please type your full name: ")
current_year = 2019
while True:
    try:
        birth_year = int(input("Please type your birth year: "))
    except ValueError:
        print("That is not a valid birth year!")
        continue
    if birth_year < 1000:
        print ("That is not a valid birth year, please try again in a four digit format!")
        continue
    if birth_year < 1910:
        print ("There is no way you are over 105 years old.")
        continue
    if birth_year > 2003:
        print ("You must be 16 years or older to use our shop.")
        continue
    if birth_year > 1910:
        break
age = current_year - birth_year

while True:
    country = str(input("Please type your country of residence: "))
    if country == correct_country:
        print("Verification Successful.")
        break
    else:
        print("Sorry, our store is currently only available to Australians")
        continue
        
# greeting
greeting = "Welcome to our newest member, "
info = "They are "
info2 = " years old."
full_greeting = greeting + name + ". " + info + str(age) + info2

print("----------------------------------------------------------")
print(full_greeting)
print("----------------------------------------------------------")

# material availability
wood = "Wood"
metal = "Metal"
wood_num = 221
metal_num = 95
full_material_message = "Our warehouse currently holds " + str(wood_num) + " pieces of " + wood + " and " + str(metal_num) + " pieces of " + metal + "."

print(full_material_message)

# cost details
cost_wood = 8.50
cost_metal = 14.20
full_cost_message = wood + " currently costs " + "$" + str(cost_wood) + " per piece and " + metal + " currently costs " + "$" + str(cost_metal) + " per piece."

print(full_cost_message)

# client order
# wood = 0
# metal = 1

print("----------------------------------------------------------")
print("Selection of material, " + wood + " = 0 and " + metal + " = 1.")
print("Available " + wood + " stock = " + str(wood_num) + ".")
print("Available " + metal + " stock = " + str(metal_num) + ".")

while True:
    try: 
        chosen_material = int(input("Please choose a material: "))
    except ValueError:
        print("That is not a valid material type, please try again!")
        continue
    if chosen_material > 1:
        print("That is not a valid material type, please try again!")
        continue
    if chosen_material == 0:
        print("You have selected " + wood + ".")
        break
    if chosen_material == 1:
        print("You have selected " + metal + ".")
        break

while True:
    try:
        amount = int(input("Please type your desired amount: "))
    except ValueError:
        print("That is not a valid number, please try again!")
        continue
    if chosen_material == 0 and amount > 221:
        print("Sorry, we do not have enough of that material in stock! Available " + wood + " stock = " + str(wood_num) + ".")
        continue
    if chosen_material == 1 and amount > 95:
        print("Sorry, we do not have enough of that material in stock! Available " + metal + " stock = " + str(metal_num) + ".")
        continue
    if chosen_material == 0 and amount < 222:
        break
    if chosen_material == 1 and amount < 96:
        break
    
# order 1 process

# wood process
if chosen_material == 0:
    total_cost = amount * cost_wood
    wood_num = wood_num - amount
    sale_message = name + " has bought " + str(amount) + " piece(s) of " + wood + " for " + "$" + str(total_cost)
    
    if amount == 1:
        sale_message = name + " has bought " + str(amount) + " piece of " + wood + " for " + "$" + str(total_cost)
    if amount == 0:
        sale_message = name + " has decided not to purchase any " + wood + "."
    if amount > 1:
        sale_message = name + " has bought " + str(amount) + " pieces of " + wood + " for " + "$" + str(total_cost)
    
    if wood_num == 1:
        stock_message = "There is now " + str(wood_num) + " piece of " + wood + " left in stock." 
    if wood_num == 0:
        stock_message = "Our " + wood + " stock is currently empty."
    if wood_num > 1:
        stock_message = "There are now " + str(wood_num) + " pieces of " + wood + " left in stock."
    
    print("----------------------------------------------------------")
    print(sale_message)
    print(stock_message)    

# metal process
else:
    if chosen_material == 1:
        total_cost = amount * cost_metal
        metal_num = metal_num - amount
        
    if amount == 1:
        sale_message = name + " has bought " + str(amount) + " piece of " + metal + " for " + "$" + str(total_cost)
    if amount == 0:
        sale_message = name + " has decided not to purchase any " + metal + "."
    if amount > 1:
        sale_message = name + " has bought " + str(amount) + " pieces of " + metal + " for " + "$" + str(total_cost)
    
    if metal_num == 1:
        stock_message = "There is now " + str(metal_num) + " piece of " + metal + " left in stock."
    if metal_num == 0:
        stock_message = "Our " + metal + " stock is currently empty."    
    if metal_num > 1:
        stock_message = "There are now " + str(metal_num) + " pieces of " + metal + " left in stock."
        

    print("----------------------------------------------------------")
    print(sale_message)
    print(stock_message)
First, learn to use if/elif/else to make a chain where only one action is taken. That way you can get rid of a lot of the continues. Frex:

while True:
    n = input('number: ')
    if n < 1:
        print('Too low.')
    elif n > 801:
        print('Too high.')
    else:
        break
The break only runs if neither of the conditions above it are True, so you don't need any continues.

Second, you should look into the format method of strings, or the even newer f-string syntax (Python 3.6+). They're simpler and clearer, and may have better efficiency.
(Apr-18-2019, 01:48 PM)ichabod801 Wrote: [ -> ]First, learn to use if/elif/else to make a chain where only one action is taken. That way you can get rid of a lot of the continues. Frex:
 while True: n = input('number: ') if n < 1: print('Too low.') elif n > 801: print('Too high.') else: break 
The break only runs if neither of the conditions above it are True, so you don't need any continues. Second, you should look into the format method of strings, or the even newer f-string syntax (Python 3.6+). They're simpler and clearer, and may have better efficiency.

Thanks heaps for the feedback, I'm going to take a deep look into this stuff when I wake up tomorrow. I really appreciate the reply, I'm going to need all the help and tips I can get.

So looking at the code you posted, n would be acceptable as somewhere between 1 and 801? So say I input the number 232, that would break the loop and could do something like print("Number confirmed.")?
(Apr-18-2019, 02:01 PM)Dash Wrote: [ -> ]So looking at the code you posted, n would be acceptable as somewhere between 1 and 801? So say I input the number 232, that would break the loop and could do something like print("Number confirmed.")?

Right, n has to be between 1 and 801 inclusive. 232 would break out of the loop. You could put a confirmation message before the break or after the whole loop, either would have the same effect. Note that 999 would run the 'too high' print statement. Then the block of code under the else statement would not execute, because one of the previous blocks in the if/elif/else chain had executed. You then get to the end of the loop code with no break, and loop back to the beginning.
Alright, here is an update on my little project. I have gone through and edited what I could with my new knowledge of else to condense some code, however I'm not very certain on when elif would be applicable. Through testing my code it seems to work the exact same as an if. Why would I use an elif instead of an if?

I'm also looking to add spelling variants to the user's input for the correct_country variable, for example a user inputs aus instead of Australia. I've looked all over and I'm not understanding the process. How would I go about doing so? Finally, is there also a way to format multiple subs with different variables in a single sentence via .format()? As in message = {name} has purchased {material}!".format().

EDIT
I think I just found the solution to my multiple formatting issue via the f-strings. If I was to do something like print(f"{name} has purchased {material}, thank you!" would that work if I had those corresponding variables defined?

# requirement
correct_country = "Australia" 
valid_year = 1910

# contact details
name = input("Please type your full name: ")
current_year = 2019
while True:
    try:
        birth_year = int(input("Please type your birth year: "))
    except ValueError:
        print("That is not a valid birth year!")
        continue
    if birth_year < valid_year:
        print ("There is no way you are over 109 years old.")
    else:
        break

# age calculation        
age = current_year - birth_year

# country input
while True:
    country = input("Please type your country of residence: ")
    if country == correct_country:
        print("Verification Successful.")
        break
    else:
        print("Sorry, our store is currently only available to Australians")
        continue
        
# greeting
greeting = "Welcome to our newest member, {0}.".format(name) + " They are {0} years old.".format(age)

print("----------------------------------------------------------")
print(greeting)
print("----------------------------------------------------------")

# material availability
wood = "Wood"
metal = "Metal"
wood_num = 221
metal_num = 95
full_material_message = "Our warehouse currently holds {0} pieces of Wood".format(wood_num) + " and {0} pieces of Metal.".format(metal_num)

print(full_material_message)

# cost details
cost_wood = 8.50
cost_metal = 14.20
full_cost_message = "Wood currently costs ${0} per piece".format(cost_wood) + " and Metal currently costs ${0} per piece.".format(cost_metal) 

print(full_cost_message)

# client order (wood = 0, metal = 1)
print("----------------------------------------------------------")
print("Selection of material, " + wood + " = 0 and " + metal + " = 1.")
print("Available Wood stock = {0}.".format(wood_num))
print("Available Metal stock = {0}.".format(metal_num))

while True:
    try: 
        chosen_material = int(input("Please choose a material: "))
    except ValueError:
        print("That is not a valid material type, please try again!")
        continue
    if chosen_material == 0:
        print("You have selected " + wood + ".")
        break
    elif chosen_material == 1:
        print("You have selected " + metal + ".")
        break
    else:
        print("That is not a valid material type, please try again!")

while True:
    try:
        amount = int(input("Please type your desired amount: "))
    except ValueError:
        print("That is not a valid number, please try again!")
        continue
    if chosen_material == 0 and amount > wood_num:
        print("Sorry, we do not have enough of that material in stock! Available Wood stock = {0}.".format(wood_num))
    elif chosen_material == 1 and amount > metal_num:
        print("Sorry, we do not have enough of that material in stock! Available Metal stock = {0}.".format(metal_num))
    else:
        break

# order process
if chosen_material == 0:
    total_cost = amount * cost_wood
    wood_num = wood_num - amount
else:
    total_cost = amount * cost_metal
    metal_num = metal_num - amount

if amount == 1 and chosen_material == 0:
    sale_message = "{0} has bought ".format(name) + "{0} piece of Wood".format(amount) + " for ${0}.".format(total_cost)
if amount == 0 and chosen_material == 0:
    sale_message = "{0} has decided not to purchase any Wood.".format(name)
if amount > 1 and chosen_material == 0:
    sale_message = "{0} has bought ".format(name) + "{0} pieces of Wood".format(amount) + " for ${0}.".format(total_cost)
    
if wood_num == 1 and chosen_material == 0:
    stock_message = "There is now {0} piece of Wood left in stock.".format(wood_num)
if wood_num == 0 and chosen_material == 0:
    stock_message = "Our Wood stock is currently empty."
if wood_num > 1 and chosen_material == 0:
    stock_message = "There are now {0} pieces of Wood left in stock.".format(wood_num)
    
if chosen_material == 0:
    print("----------------------------------------------------------")
    print(sale_message)
    print(stock_message)    
        
if amount == 1 and chosen_material == 1:
    sale_message = "{0} has bought ".format(name) + "{0} piece of Metal".format(amount) + " for ${0}.".format(total_cost)
if amount == 0 and chosen_material == 1:
    sale_message = "{0} has decided not to purchase any Metal.".format(name)
if amount > 1 and chosen_material == 1:
    sale_message = "{0} has bought ".format(name) + "{0} pieces of Metal".format(amount) + " for ${0}.".format(total_cost)
    
if metal_num == 1 and chosen_material == 1:
    stock_message = "There is now {0} piece of Metal left in stock.".format(metal_num)
if metal_num == 0 and chosen_material == 1:
    stock_message = "Our Metal stock is currently empty."
if metal_num > 1 and chosen_material == 1:
    stock_message = "There are now {0} pieces of Metal left in stock.".format(metal_num)

if chosen_material == 1:
    print("----------------------------------------------------------")
    print(sale_message)
    print(stock_message)
(Apr-19-2019, 09:09 AM)Dash Wrote: [ -> ]EDIT I think I just found the solution to my multiple formatting issue via the f-strings. If I was to do something like print(f"{name} has purchased {material}, thank you!" would that work if I had those corresponding variables defined?

Ran through my code and re-did all of the old formatting to the new f-string formatting, and holy gosh is it amazing. So much cleaner and easier to use.