Understand and be able to reasonably define the following terms:
# assume the user enters 6
num = int(input("Enter a number: "))
total = num * 10
print(total)
# assume the user enters 'Dogs'
message = input("Enter a message: "))
print(message[3])
x = 7
y = x + 1
y = y * 2
z = x + y
print(z)
a = 3
b = 4
c = a + 2 * (b + 5)
print(c)
What is wrong with the following programs?
cost = input("Enter the cost: ")
tax = 0.053 * cost
print("Tax is" tax)
input("Enter your name: ")
print("Hello", name)
length = int(input("Enter length: "))
width = int(input("Enter width: ")
length * width = area
print(area)
age = 2019 - born
born = float(input("Enter year you were born: "))
print("You are about", age, "years old.")
Writing code
name = input("What's your name? ")
if len(name) < 5:
print("My you have a short name")
print(name, "is a good name though!")
print("Thanks for running the program", name)
x = 7
y = 2
if y != 2:
print("Apple")
elif x == 7:
print("Banana")
else:
print("Pear")
temp = 72
if temp < 20:
print("Super cold")
elif temp < 60:
print("Cold")
elif temp < 80:
print("Nice")
elif temp < 90:
print("Warm")
else:
print("Hot")
temp = 72
if temp < 20:
print("Super cold")
if temp < 60:
print("Cold")
if temp < 80:
print("Nice")
if temp < 90:
print("Warm")
else:
print("Hot")
age = int(input("How old are you? "))
height = int(input("How tall are you? "))
if age > 10 and height > 48:
print("You can ride the roller coaster")
else:
print("Sorry you can't ride")
If the user enters:
# this is the same program, except the and is an or
age = int(input("How old are you? "))
height = int(input("How tall are you? "))
if age > 10 or height > 48:
print("You can ride the roller coaster")
else:
print("Sorry you can't ride")
If the user enters:
Writing programs
# assume the user enters the numbers "7 3 2 -1"
value = float(input("Enter a number: "))
prod = 1
while value > 0:
prod = prod * value
value = float(input("Enter a number: "))
print(prod)
number = 7
times = 0
while number > 0:
number = number - 2
times = times + 1
print(times)
# assume the user enters 5
start = int(input("Enter start: "))
for num in range(start, 0, -1):
print(num, "...")
print("Blastoff!")
# assume the user enters 5
end = int(input("Enter end: "))
tot = 0
for i in range(1, val + 1):
tot = tot + i
print(tot)
Writing Programs
Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.