hw 20/3/25 11:31 AM
This commit is contained in:
parent
41aa105ca1
commit
d91b49ddcb
3 changed files with 65 additions and 0 deletions
21
py/MoreIfStatements/challenge1.py
Normal file
21
py/MoreIfStatements/challenge1.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
# Ask the user to enter a number
|
||||
num1 = int(input("Enter a number: "))
|
||||
|
||||
# Check if the first number is 5
|
||||
if num1 == 5:
|
||||
print("You win")
|
||||
else:
|
||||
# If the first number is greater than 5
|
||||
if num1 > 5:
|
||||
print("Too high, try again")
|
||||
else:
|
||||
print("Too low, try again")
|
||||
|
||||
# Ask the user to enter another number
|
||||
num2 = int(input("Enter another number: "))
|
||||
|
||||
# Check if the second number is 5
|
||||
if num2 == 5:
|
||||
print("You win")
|
||||
else:
|
||||
print("You lose")
|
32
py/MoreIfStatements/challenge2.py
Normal file
32
py/MoreIfStatements/challenge2.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
# Start of the program
|
||||
|
||||
# Prompt the user to enter the first number and convert it to an integer
|
||||
num1 = int(input("Enter num1: "))
|
||||
|
||||
# Prompt the user to enter the second number and convert it to an integer
|
||||
num2 = int(input("Enter num2: "))
|
||||
|
||||
# Calculate the sum of the two numbers
|
||||
answer = num1 + num2
|
||||
|
||||
# Check if the sum is less than 20
|
||||
if answer < 20:
|
||||
# If the sum is less than 20, print the sum
|
||||
print("Output:", answer)
|
||||
|
||||
# If the sum is exactly 20
|
||||
elif answer == 20:
|
||||
# Print the message indicating the total is 20
|
||||
print("Output: The total is 20")
|
||||
|
||||
# If the sum is greater than 20
|
||||
else:
|
||||
# Multiply the sum by 3 as per the flowchart
|
||||
answer *= 3
|
||||
# Print the modified answer
|
||||
print("Output:", answer)
|
||||
|
||||
# Print the goodbye message as the final step
|
||||
print("Output: Goodbye")
|
||||
|
||||
# End of the program
|
12
py/MoreIfStatements/challenge4.py
Normal file
12
py/MoreIfStatements/challenge4.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
# Ask the user to select a color
|
||||
color = input("Select a color (red, green, or blue): ").strip().lower()
|
||||
|
||||
# Check the user's input and display the appropriate message
|
||||
if color in ["red", "r"]:
|
||||
print("You selected red.")
|
||||
elif color in ["green", "g"]:
|
||||
print("You selected green.")
|
||||
elif color in ["blue", "b"]:
|
||||
print("You selected blue.")
|
||||
else:
|
||||
print("Incorrect choice.")
|
Loading…
Add table
Reference in a new issue