I know break doesn't work in the if statement,

Asked 2 years ago, Updated 2 years ago, 18 views

Question 4
The criteria for obtaining a type 2 ordinary driver's license are as follows.
1) writeten_test (department test) 60 points or more
2) function_test (function test) 80 points or more
3) road_test (road driving) 70 points or more

If you pass the department test, skill test, and road driving step by step, you will obtain a driver's license.
If the academic test is less than 60 points, it is rejected and you cannot take a skill test. If the skill test is less than 80 points, you will be rejected and you will not be able to take the road test The result of road driving is either obtaining a driver's license or failing.

Enter your score and create a code to determine whether you passed the driver's license test.

Result Example)

Let's find out if you get a type 2 normal driver's license.
Please enter your academic exam results.
60
Please enter your skill test scores.
70
Failed
Please print it out like the execution result below.
Execution result *
Let's find out if you get a type 2 normal driver's license.
Please enter your academic exam results.
70
Please enter your skill test scores.
80
Please enter your road test scores.
75
Obtaining a driver's license

My programming(?) is

print("Let's find out if you have obtained a type 2 normal driver's license.")
print("Please enter your department test results.")
written_test=int(input())
if written_test>=60:
    print("Enter your skill test scores.")
else: 
    print ("Fail")  


function_test=int(input())
if function_test>=80:
    print("Please enter your road test scores.")
else: 
    print ("Fail")


road_test=int(input())
if road_test>=70:
    print ("Getting a Driver's License")
else:
    print ("Fail")

I have to fail and stop, but I keep moving on to the next instruction. What should I do in this case?

python

2022-09-21 15:45

3 Answers

The person above suggested a method using a function and a return statement (actually, this is desirable), but I think you haven't learned the function yet, so I'll introduce a method using a while statement and a break statement that can break through the repetition sentence.

The code below is the example code I wrote. To make it easier for you to understand, I purposely wrote the variable name in Korean :3

If you have any questions, please leave a comment!

( 변수 If InvalidSyntaxError appears in the variable declaration part, the Python version may not support the Korean variable name. Please change it to an appropriate English name!)

#-*- Coding: UTF-8 -*-
#Setting the default value of acquisition before receiving test scores before receiving them
Whether to obtain a driver's license = False;

while (True):

    #Check if your written test score is less than 60.
    Written test scores = int("Enter your written test scores" >> "))

    if (written test results < 60):
        #Notice of failure and break through the repeat door.
        print ("[!] written test failed.");
        break;


    #Check if the actual test score is less than 80.
    Practical test scores = int("Enter practical test scores" >> "))

    if (Practical test results < 80):
        #Notice of failure and break through the repeat door.
        print ("[!] failed the practical test.");
        break;


    #See if your road test scores are below 70.
    Road test results = int (input ("road test results") >> "))

    if (road driving performance < 70):
        #Notification of failure.
        print ("[!] road test failed.");
    else:
        Driver's license acquisition status = True;

    #Whether you got it or not, once you've come this far, you have to finish the work,
    #Break through the repeat door.
    break; 

print("\n[?] Whether to obtain a driver's license or not: " + str (whether to obtain a driver's license or not))



2022-09-21 15:45

Just to answer your question: Cover it with a function and do something return where you want it. Then the function stops there.

def driving_test() :
    print("Let's find out if you have obtained a type 2 normal driver's license.")
    print("Please enter your department test results.")
    written_test=int(input())
    if written_test>=60:
        print("Enter your skill test scores.")
    else: 
        print ("Fail")
        return False
# a sneak attack

To be flattered: If you read the question, you'll see this.

If the academic test is less than 60 points, it is rejected and you cannot take a skill test.
If the skill test is less than 80 points, you will be rejected and you will not be able to see the road trip The result of road driving is either obtaining a driver's license or failing.

This text makes it a little embarrassing to see what kind of conditional statement this problem wants.

 If the department test is less than 60 points:
    a failure
[If not:]
    [You can take a skill test]
    [Taking a skill test]
    If the skill test is less than 80 points:
        a failure
    [If not:]
        [You can see the road driving]
        [Looking at the road driving]
        If the road test result is less than 70 points:
            a failure
        [If not:]
            Obtaining a driver's license

I hope it's useful.


2022-09-21 15:45

Can't we combine the if statements? Is there a reason why you use it?


2022-09-21 15:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.