简体   繁体   中英

if statement inside a function and print out specifice value after user input

Hi I have created a function and it doesn't work when I call it up and apply in the final print. Code is here:

def adjust(cents):
    if ( cents ==1 or cents == 2):
         cents =0
    elif (cents == 3 or cents ==4 or cents ==6 or cents ==7):
         cents =5
    elif ( cents ==8 or cents ==9):
         cents = 10
    return cents

cents =(input("Enter a number of cents between 0 and 9:"))

print("The final round of number is:" + adjust(cents))

The rule is pretty clean, all it have to do is just to take and read the user input and print the round up value follwing the rules.

The problem is you're taking input as a string but compare it with an integer. Take input as an integer then compares it with your condition.

def adjust(cents):
    if (cents == 1 or cents == 2):
        cents = 0
    elif (cents == 3 or cents == 4 or cents == 6 or cents == 7):
        cents = 5
    elif (cents == 8 or cents == 9):
        cents = 10
    return cents


cents = int(input("Enter a number of cents between 0 and 9:"))

print(f"The final round of number is: {adjust(cents)}")

More pythonic way

def adjust(cents):
    if cents in [1,2]:
        cents = 0
    elif cents in [3,4,6,7]:
        cents = 5
    elif cents in [8,9]:
        cents = 10
    return cents

EDIT : As per @Roy suggestion it is also easier to use dictionary.

my_dict = {1: 0, 2: 0, 3: 5, 4: 5, 6: 5, 7: 5, 8: 10, 9: 10}

cents = int(input("Enter a number of cents between 0 and 9:"))
print(f"The final round of number is: {my_dict[cents]}")

mhhabib is right, but I also wanted to point out you can simplify/clean up those conditional statements. You can also return as soon as a condition is met rather than making additional comparisons, by just returning instead of assigning cents, eg if cents is either 1 or 2, then it can't be 3, 4, 6, or 7 so there's no sense checking the other conditions rather than immediately returning.

def adjust(cents):
    if cents in [1, 2]:
        return 0
    if cents in [3, 4, 6, 7]:
        return 5
    if cents in [8, 9]:
        return 10


cents = int(input("Enter a number of cents between 0 and 9: "))
print(f"The final round of number is: {adjust(cents)}")

You might also want to check that the number the user entered is actually between 0 and 9:

cents = int(input("Enter a number of cents between 0 and 9: "))
while True:
    if cents > 9 or cents < 0:
        cents = int(input("Enter a number of cents between 0 and 9: "))
    else:
        break

print(f"The final round of number is: {adjust(cents}")

mhhabib added the "if cents in list" method as an edit after I had already submitted this, but I also included the option to return rather than checking other conditions unnecessarily and also the bit about actually checking if the input matched what is expected, eg what happens in the other examples if the user entered 10?

You need to convert the user input string to number. After processing the rule, print the round up value as string.

def adjust(cents):
if ( cents ==1 or cents == 2):
     cents =0
elif (cents == 3 or cents ==4 or cents ==6 or cents ==7):
     cents =5
elif ( cents ==8 or cents ==9):
     cents = 10
return cents

cents = int(input("Enter a number of cents between 0 and 9: "))

print("The final round of number is:" + str(adjust(cents)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM