Sie sind auf Seite 1von 2

Task 20

Looking at the code below, explain why the LOGIC is wrong in this example.
1 # mega sale
2 salesAmount = float(input(“Enter the amount of money spent in £: “))
3 if saleAmount > 10:
4 print(“You qualify for a £1 voucher”)
5 if salesAmout > 20:
6 print(“You qualify for a £3 voucher”)
7 else:
a. print(“You didn’t spent enough for a voucher!”)
Answer:
If an if statement is true, it will perform the statement inside it then moves to the next line, however, in
this case, two if statements are used independently, so if both conditions are correct, e.g. the number entered is
greater than 10 and 20, both messages (“You qualify for a £1 voucher” and “You qualify for a £3 voucher”) will
be printed out.

Looking at the code below, explain why the LOGIC is wrong in this example.
1 # mega sale
2 salesAmount = float(input(“Enter the amount of money spent in £: “))
3 if saleAmount > 10:
4 print(“You qualify for a £1 voucher”)
5 elif salesAmout > 20:
6 print(“You qualify for a £3 voucher”)
7 else:
a. print(“You didn’t spent enough for a voucher!”)
Answer:
When using an if statement followed by elif and else, only one statement should be true. The program
executes and checks each statement one by one from to top bottom. The problem with this logic is that once a
statement from one of the if, elif and else statements is true, the program skips the next proceeding elif and else
statements and goes straight to the print line. The problem occurs when the entered number is larger than 10, the
condition within elif and else statements will be skipped the only message that will be printed out is the message
for when salesAmount is greater than 10, neglecting whether the number was greater than 20.

Looking at the code below, explain why the LOGIC will work in this example.
1 # mega sale
2 salesAmount = float(input(“Enter the amount of money spent in £: “))
3 if saleAmount > 20:
4 print(“You qualify for a £1 voucher”)
5 elif salesAmout > 10:
6 print(“You qualify for a £3 voucher”)
7 else:
a. print(“You didn’t spent enough for a voucher!”)
Answer:
This logic will work because with the order of the conditions, if a number is greater than 10 and less
than 20, it will pass through the first statement, rather than being skipped.
Task 29:
Why is the int necessary in line 3?
This is to make sure the user will input the right type of data. since we’re dealing with whole numbers
from 1 to 10, the data type should be int.

Validation
# Enter a number between 1 and 10
1 number = 40
2 while number not in range (1,11):
3 try:
4 number = int(input(“{Please enter a number between 1 and 10”))
5 except:
6 print(“ERROR invalid input. Out of range or wrong type of data.”)
7 print(“Thank you I have recorded your entry as:”, number)
Explain what each line of codes does:
1 – initializes a number for triggering the while loop
2 – Creates the while loop and range of inputs
3 – initiates try block which will transfer to except block when error is encountered.
4 – lets user input an integer
5 – initiates except block which will execute when there is an error in the try block
6 – Error message when error is encountered in try block
8 – message when there is no error in try block and while loop satisfied.

Task 31

We can use a truth table (just like gates) to see what the permutations are:
Rock = 1, Paper = 2, Scissors = 3

User Choice Number Computer Choice Number Scoring


Rock 1 Rock 1 User +=1 / Computer +=1
Rock 1 Paper 2 User += 0 / Computer +=1
Rock 1 Scissors 3 User += 1 / Computer +=0
Paper 2 Rock 1 User += 1 / Computer +=0
Paper 2 Paper 2 User += 1 / Computer +=1
Paper 2 Scissors 3 User += 0 / Computer +=1
Scissors 3 Rock 1 User += 0 / Computer +=1
Scissors 3 Paper 2 User +=1 / Computer += 0
Scissors 3 Scissors 3 User +=1 / Computer +=1

Das könnte Ihnen auch gefallen