Home > Mobile >  is there a way to make a piece of text run if something deosn't get run
is there a way to make a piece of text run if something deosn't get run

Time:03-17

I am making a code that when the user is asked what the temperature is depending on the temperature it will tell the user how many ice-creams would have been sold. After that it will ask what type of day it is (weekend or weekday).

However I would like to make it so that if the user puts in a temperature that is over 45 for example it outputs an error message but i would like to make it so when that is outputted the asking if it a weekend or weekday question isn't asked but if that error message isn't run the then day questions to run because when the error message is run there is no ice-cream total stored so the day type outputs wouldn't work. I hope you understand what I am trying to ask :). Here is my code so far it all works other than my problem so far:

#this code will ask the suer for the temperature and type of day and then wil tell them how many icecreames would have been sold on those days.

    import time

    temptype=int(input("Enter the temperature for that day: "))

if temptype >=20 and temptype <=30:
        time.sleep(0.3)
        icecream=100
        print("The ammount of icecream that were sold are:",icecream)

elif temptype >=31 and temptype <=37:
        time.sleep(0.3)
        icecream=150
        print("The ammount of icecream that were sold are:",icecream)

elif temptype >=38 and temptype <=45:
        time.sleep(0.3)
        icecream=120
        print("The ammount of icecream that were sold are:",icecream)

else:
        time.sleep(0.3)
        error=("Error please restart the program")
        print(error)

time.sleep(0.3)
print("----------------------------------------------------")

daytype=input("Enter the type of day. Weekend or Weekday:")

if daytype == "weekend":
        time.sleep(0.3)
        ictotal=icecream*2
        print("The ammont of icecream that were sold are:",ictotal)

elif daytype =="weekday":
        time.sleep(0.3)
        print("The ammont of icecream that were sold are:",icecream)

else:
    time.sleep(0.3)
    print("Error please restart the program")

CodePudding user response:

You can simply add another if to determine if the lower section of code should run:

import time

temptype=int(input("Enter the temperature for that day: "))

if temptype >=20 and temptype <=30:
    time.sleep(0.3)
    icecream=100
    print("The ammount of icecream that were sold are:",icecream)

elif temptype >=31 and temptype <=37:
    time.sleep(0.3)
    icecream=150
    print("The ammount of icecream that were sold are:",icecream)

elif temptype >=38 and temptype <=45:
    time.sleep(0.3)
    icecream=120
    print("The ammount of icecream that were sold are:",icecream)

else:
    time.sleep(0.3)
    error=("Error please restart the program")
    print(error)

if temptype <= 45 or temptype < 20: #notice the added statement here

    time.sleep(0.3)
    print("----------------------------------------------------")

    daytype=input("Enter the type of day. Weekend or Weekday:")

    if daytype == "weekend":
        time.sleep(0.3)
        ictotal=icecream*2
        print("The ammont of icecream that were sold are:",ictotal)

    elif daytype =="weekday":
        time.sleep(0.3)
        print("The ammont of icecream that were sold are:",icecream)

    else:
        time.sleep(0.3)
        print("Error please restart the program")

This means that everything after the error will not run when the error runs, but will run otherwise.

  • Related