简体   繁体   中英

How do I ask for an input from a user and check that value within a range to give me the correct response

So my professor for python assigned an exercise which was to create a program to accept a user input or "score" and create an if else loop with an if else nested loop inside the first loop to check if the score is greater than or equal to 80. If that's satisfied, print a statement saying "you passed." and if the score is equal to 90 or more, then it should say"you got an A." and if the score is below an 80, it should say "you failed."

so far, this is the code I was able to come up with. And it runs up until the score entered is anything equal to or more than 90.

 userScore = int(input('enter score:\n')) if userScore < 80: userScore = int(input('you failed. Try again:\n')) if userScore == 80 or userScore > 80: print(f'good job, you got a B.') elif userScore >= 90: print('you got an A') else: print('enter a differnt score')

enter code here

as per your question your code should be:

score = int(input("Please enter your score: "))
if score >= 80:
    print("You passed.")
    if score >= 90:
        print("You got an A.")
else:
    print("You failed.")

while(1):
    score = int(input("Please enter your score: "))
    if score >= 80:
        print("You passed.")
        if score >= 90:
            print("You got an A.")
            break
    else:
        print("You failed.")
while(1):
score = int(input("Please enter your score pal: "))
if score >= 80:
    print("You passed the exam.")
    if score >= 90:
        print("You got an A in the exam.")
        break
else:
    print("You failed the exam.")

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