SkylerS
SkylerS

Reputation: 1

How do I skip a line of code if a condition is true?

When I enter a number that isn't between 1 and 7 it says "Error! Please enter a number (1-7) for the day of the week:" but then after something is entered it says "Enter a number (1-7) for the day of the week:". I want "Error! Please enter a number (1-7) for the day of the week:" to replace "Enter a number (1-7) for the day of the week:" when a number that isn't between 1-7 is entered. Something I tried earlier was to just put print(error!) after else but I didn't like that error! appeared above enter a number. Is there a way I could make them appear on the same line? By the way, my professor requires that I use an if-elif-else statement and that the code is a continuous loop.

keep_going = 'y'
while keep_going == 'y':
    day = int(input('Enter a number (1-7) for the day of the week:'))
    if  day == 1:
        print('Monday')
    elif day == 2:
        print('Tuesday')
    elif day == 3:
        print('Wednesday')
    elif day == 4:
        print('Thursday')
    elif day == 5:
        print('Friday')
    elif day == 6:
        print('Saturday, Happy Weekend!')
    elif day == 7:
        print('Sunday, Happy Weekend!')
    else:
        int(input('Error! Please enter a number (1-7) for the day of the week:'))

Upvotes: 0

Views: 2754

Answers (4)

Steven
Steven

Reputation: 2133

The best solution is to just change the last lines of code to:

    else:
        print('Error! ', end='')

Control will then flow back to the beginning of the loop and the "Enter a number" prompt will double as an explanation for the error and a prompt for a new number.

This way your form remains consistent so that all if branches do a print and nothing else.

Upvotes: 0

DYZ
DYZ

Reputation: 57033

You forgot an assignment on the last line of the code. The value of day is never updated.

day = int(....

Incidentally, you never update the value of keep_going, either. Your loop never stops. Finally, you should use a list of day names and access the names by index instead of having an 8-way if statement.

Upvotes: 1

MNM
MNM

Reputation: 2743

Here is a nice method that can do what you want instead of a whole bunch of if-else statements. It will make the code a little cleaner and easier to maintain and update.

number = 1 
num2 = 8 

def check_number(num):
    if 0<num<8:
        print("ok number")
    else:
        print("Error")

check_number(1)
check_number(8)

Upvotes: 0

it&#39;s-yer-boy-chet
it&#39;s-yer-boy-chet

Reputation: 2017

Some stylistic tweaks (my opinion I know) and added the missing variable assignment of day and an exit condition for the while loop.

keep_going = True
response = dict(zip(
    range(1,8), (
         'Monday',
         'Tuesday',
         'Wednesday',
         'Thursday',
         'Friday',
         'Saturday, Happy Weekend!',
         'Sunday, Happy Weekend!'
    )
))
while keep_going:
    day = int(input('Enter a number (1-7) for the day of the week:'))
    if  day in response:
        print(response[day])
        keep_going = False
    else:
        day = int(input('Error! Please enter a number (1-7) for the day of the week:'))

Upvotes: 0

Related Questions