food = input("What would you like to eat? ")
if food == "pizza":
reply = "Yummy"
else:
reply = "What's that?"
print(reply)
What would you like to eat? pizza Yummy
food = input("What would you like to eat? ")
if food == "pizza":
reply = "Yummy"
else:
reply = "What's that?"
print(reply)
What would you like to eat? fries What's that?
weather = input("Is it raining? ").capitalize()
if weather == "Yes":
print("Yay!")
else:
print("Yucks")
print("I'm only happy when it rains.")
Is it raining? Yes Yay! I'm only happy when it rains.
weather = input("Is it raining? ").capitalize()
if weather == "Yes":
print("Yay!")
else:
print("Yucks")
print("I'm only happy when it rains.")
Is it raining? No Yucks I'm only happy when it rains.
age = int(input("How old are you? "))
if age <= 8:
print("Kid's price.")
elif 8 <= age < 16:
print("25% off.")
else:
print("Regular price")
How old are you? 6 Kid's price.
age = int(input("How old are you? "))
if age <= 8:
print("Kid's price.")
elif 8 <= age < 16:
print("25% off.")
else:
print("Regular price")
How old are you? 15 25% off.
price = 1000000
good_credit = True
if good_credit:
down_payment = price * 0.2
else:
down_payment = price * 0.25
print(f"Down payment = ${down_payment}")
Down payment = $200000.0
name = input("Create your log in ID: ")
if len(name) < 5:
print("Name must be at least 5 characters.")
elif len(name) > 20:
print("Name cannot exceed 20 characters.")
else:
print("The force is strong with your name.")
Create your log in ID: jedimaster The force is strong with your name.
beer = float(input("Volume of beer needed: "))
unit = input("(G)allon or (L)itre: ")
if unit.upper() == "G":
converted = beer / 0.26417
print(f"That is {converted} litres")
else:
converted = beer * 0.26417
print(f"That is {converted} gallons.")
print("The source and solution to all of life's problems. Heh Heh Heh.")
Volume of beer needed: 6 (G)allon or (L)itre: G That is 22.712647159026382 litres The source and solution to all of life's problems. Heh Heh Heh.
for i in range(6):
print(i)
0 1 2 3 4 5
for i in range(2, 8):
print(i)
2 3 4 5 6 7
for i in range(3, 6):
print(i + 2)
5 6 7
for i in range(6, 1, -1):
print(i)
6 5 4 3 2
for i in range(6, 1, -1):
print(3 - i)
-3 -2 -1 0 1
i = 0
while i <= 5:
print(i)
i = i + 1
0 1 2 3 4 5
i = 10
while i <= 15:
print (i)
i += 1
else:
print("i is now more than 15")
10 11 12 13 14 15 i is now more than 15
Use break to stop the loop even if the condition is true.
i = 5
while i < 10:
print(i)
if i == 7:
break
i += 1
5 6 7
Use continue to skip a value.
i = 5
while i < 10:
i += 1
if i == 7:
continue
print(i)
6 8 9 10
number = int(input("Enter an integer greater than zero "))
factorial = 1
for i in range(2, number + 1):
factorial = factorial * i
print(str(number) + " factorial is " + str(factorial))
Enter an integer greater than zero 5 5 factorial is 120
num = 6.1
num = int(num)
num
6
number = input("Enter a number greater than zero ")
if isinstance(number, int) == True:
number = number
else:
number = int(float(number))
if number <= 0:
print("The force is weak with zero and negative numbers.")
else:
factorial = 1
for i in range(2, number + 1):
if number <= 0:
break
factorial = factorial * i
print(str(number) + " factorial is " + str(factorial))
Enter a number greater than zero 5.123 5 factorial is 120
number = input("Enter a number greater than zero ")
if isinstance(number, int) == True:
number = number
else:
number = int(float(number))
if number <= 0:
print("The force is weak with zero and negative numbers.")
else:
factorial = 1
for i in range(2, number + 1):
if number <= 0:
break
factorial = factorial * i
print(str(number) + " factorial is " + str(factorial))
Enter a number greater than zero -2 The force is weak with zero and negative numbers.
number = input("Enter an integer greater than zero ")
if isinstance(number, int) == True:
number = number
else:
number = int(float(number))
if number <= 0:
print("The force is weak with zero and negative numbers.")
else:
factorial = 1
i = 2
while i <= number:
factorial = factorial * i
i += 1
print(str(number) + " factorial is " + str(factorial))
Enter an integer greater than zero 5.123 5 factorial is 120
number = input("Enter an integer greater than zero ")
if isinstance(number, int) == True:
number = number
else:
number = int(float(number))
if number <= 0:
print("The force is weak with zero and negative numbers.")
else:
factorial = 1
i = 2
while i <= number:
factorial = factorial * i
i += 1
print(str(number) + " factorial is " + str(factorial))
Enter an integer greater than zero -2 The force is weak with zero and negative numbers.
number = int(input("Add how many numbers? "))
total = 0
for i in range(number):
sum = input("Enter number " + str(i + 1) + ": ")
total += int(sum)
print("The total is " + str(total) + ". ")
Add how many numbers? 3 Enter number 1: 1 Enter number 2: 2 Enter number 3: 3 The total is 6.
number = int(input("Add how many numbers? "))
total = 0
i = 1
while i <= number:
sum = input("Enter number " + str(i) + ": ")
total += int(sum)
i += 1
print("The total is " + str(total) + ". ")
Add how many numbers? 3 Enter number 1: 1 Enter number 2: 2 Enter number 3: 3 The total is 6.
total = 0
sum = input("Enter a number; if you're done, enter 'done': ")
while sum != "done":
number = int(sum)
total = total + number
sum = input("Enter a number; if you're done, enter 'done': ")
print("The total is " + str(total) + ".")
Enter a number; if you're done, enter 'done': 1 Enter a number; if you're done, enter 'done': 2 Enter a number; if you're done, enter 'done': 3 Enter a number; if you're done, enter 'done': done The total is 6.