לדלג לתוכן

2.1 בקרת זרימה פתרון

בדיקת שפיות

# Create a program that get's a number, and allow it only be only from 1 to 10. Else, it will print a message.

number = int(input("Enter a number (1-10): "))

if number > 10 or number < 1:
    print("Number can be only from (1-10)!")
else:
    print("Good number!")

מקבל החלטות פשוט

# Create a program that asks the user for their age and tells them if they are a child, teenager, adult, or senior.

age = int(input("Enter your age: "))
maturity = ""

if age =< 12:
    maturity = "child"
elif age =< 20:
    maturity = "teenager"
elif age =< 60:
    maturity = "adult"
else:
    maturity = "senior"

print(f"You are a {maturity}")

- אפשר לפתור את התרגיל גם עם match

מחשבון 0.1

# Write a calculator that accepts an operation on a number like: 1. Addition 2. Subtraction 3. Multiplication 4. Division, receives 2 numbers to perform the operation on, then prints the result.
first_number = int(input("Enter the first number: "))
second_number = int(input("Enter the second number: "))
operation = input("Enter operation (+, -, *, /): ")
result = 0

print(f"The selected operation is {first_number} {operation} {second_number}! ")

match operation:
    case "+":
        result = first_number + second_number
    case "-":
        result = first_number - second_number
    case "*":
        result = first_number * second_number
    case "/":
        result = first_number / second_number
    case _:
        print(f"Invalid operation. {operation}")
        result = -9999999

print(f"Result = {result}")

פולינדרום

s = input("enter a palindrom string: ")
if s == s[::-1]
    print("this string is a palindrom!")
else:
    print("this string isn't a palindrom..")