import random
# Generate random numbers
number1 = random.randint(0, 9)
number2 = random.randint(0, 9)
# Prompt the user to enter an answer
answer = eval(input("What is " + str(number1) + " + "
+ str(number2) + "? "))
# Display result
print(number1, "+", number2, "=", answer,
"is", number1 + number2 == answer)
What is 3 + 8? 10 3 + 8 = 10 is False
int("3232")
3232
eval("555")
555
eval("45.55")
45.55
int("4+7")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [22], in <cell line: 1>() ----> 1 int("4+7") ValueError: invalid literal for int() with base 10: '4+7'
import random
a = random.randint(0,255)
chr(a)
'Î'
number = eval(input("Enter an integer: "))
if number % 5 == 0 :
print("HiFive");
if number % 2 == 0 :
print("HiEven");
Enter an integer: 10 HiFive HiEven
day = 0 # birth day to be determined
# Prompt the user to answer the first question
question1 = "Is your birthday in Set1?\n" + \
" 1 3 5 7\n" + \
" 9 11 13 15\n" + \
"17 19 21 23\n" + \
"25 27 29 31" + \
"\nEnter 0 for No and 1 for Yes: "
answer = eval(input(question1))
if answer == 1:
day += 1
# Prompt the user to answer the second question
question2 = "Is your birthday in Set2?\n" + \
" 2 3 6 7\n" + \
"10 11 14 15\n" + \
"18 19 22 23\n" + \
"26 27 30 31" + \
"\nEnter 0 for No and 1 for Yes: "
answer = eval(input(question2))
if answer == 1:
day += 2
# Prompt the user to answer the third question
question3 = "Is your birthday in Set3?\n" + \
" 4 5 6 7\n" + \
"12 13 14 15\n" + \
"20 21 22 23\n" + \
"28 29 30 31" + \
"\nEnter 0 for No and 1 for Yes: "
answer = eval(input(question3))
if answer == 1:
day += 4
# Prompt the user to answer the fourth question
question4 = "Is your birthday in Set4?\n" + \
" 8 9 10 11\n" + \
"12 13 14 15\n" + \
"24 25 26 27\n" + \
"28 29 30 31" + \
"\nEnter 0 for No and 1 for Yes: "
answer = eval(input(question4))
if answer == 1:
day += 8
# Prompt the user to answer the fifth question
question5 = "Is your birthday in Set5?\n" + \
"16 17 18 19\n" + \
"20 21 22 23\n" + \
"24 25 26 27\n" + \
"28 29 30 31" + \
"\nEnter 0 for No and 1 for Yes: "
answer = eval(input(question5))
if answer == 1:
day += 16
print("\nYour birthday is" + str(day) + "!")
Is your birthday in Set1? 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 Enter 0 for No and 1 for Yes: 1 Is your birthday in Set2? 2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 Enter 0 for No and 1 for Yes: 0 Is your birthday in Set3? 4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 Enter 0 for No and 1 for Yes: 1 Is your birthday in Set4? 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 Enter 0 for No and 1 for Yes: 1 Is your birthday in Set5? 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Enter 0 for No and 1 for Yes: 1 Your birthday is29!
import random
# 1. Generate two random single-digit integers
number1 = random.randint(0, 9)
number2 = random.randint(0, 9)
# 2. If number1 < number2, swap number1 with number2
if number1 < number2:
number1, number2 = number2, number1 # Simultaneous assignment
# 4. Prompt the student to answer "what is number1 - number2?"
answer = eval(input("What is " + str(number1) + " - " +
str(number2) + "? "))
# 4. Grade the answer and display the result
if number1 - number2 == answer:
print("You are correct!")
else:
print("Your answer is wrong.\n", number1, "-",
number2, "is", number1 - number2)
What is 7 - 1? 4 Your answer is wrong. 7 - 1 is 6
a = 7
b = 9
a, b = b, a
print(a,b)
9 7
year = eval(input("Enter a year: "))
zodiacYear = year % 12
if zodiacYear == 0:
print("monkey")
elif zodiacYear == 1:
print("rooster")
elif zodiacYear == 2:
print("dog")
elif zodiacYear == 3:
print("pig")
elif zodiacYear == 4:
print("rat")
elif zodiacYear == 5:
print("ox")
elif zodiacYear == 6:
print("tiger")
elif zodiacYear == 7:
print("rabbit")
elif zodiacYear == 8:
print("dragon")
elif zodiacYear == 9:
print("snake")
elif zodiacYear == 10:
print("horse")
else:
print("sheep")
Enter a year: 2022 tiger
r1 = -20
if r1 >= 0:
area1 = r1 * r1 * 3.14
print(area1)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [54], in <cell line: 4>() 2 if r1 >= 0: 3 area1 = r1 * r1 * 3.14 ----> 4 print(area1) NameError: name 'area1' is not defined
# Prompt the user to enter weight in pounds
weight = eval(input("Enter weight in pounds: "))
# Prompt the user to enter height in inches
height = eval(input("Enter height in inches: "))
KILOGRAMS_PER_POUND = 0.45359237 # Constant
METERS_PER_INCH = 0.0254 # Constant
# Compute BMI
weightInKilograms = weight * KILOGRAMS_PER_POUND
heightInMeters = height * METERS_PER_INCH
bmi = weightInKilograms / (heightInMeters * heightInMeters)
# Display result
print("BMI is", format(bmi, ".2f"))
if bmi < 18.5:
print("Underweight")
elif bmi < 25:
print("Normal")
elif bmi < 30:
print("Overweight")
else:
print("Obese")
Enter weight in pounds: 22 Enter height in inches: 22 BMI is 31.96 Obese
import sys
# Prompt the user to enter filing status
status = eval(input(
"(0-single filer, 1-married jointly,\n" +
"2-married separately, 3-head of household)\n" +
"Enter the filing status: "))
# Prompt the user to enter taxable income
income = eval(input("Enter the taxable income: "))
# Compute tax
tax = 0
if status == 0: # Compute tax for single filers
if income <= 8350:
tax = income * 0.10
elif income <= 33950:
tax = 8350 * 0.10 + (income - 8350) * 0.15
elif income <= 82250:
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
(income - 33950) * 0.25
elif income <= 171550:
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
(82250 - 33950) * 0.25 + (income - 82250) * 0.28
elif income <= 372950:
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + \
(income - 171550) * 0.33
else:
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + \
(372950 - 171550) * 0.33 + (income - 372950) * 0.35;
elif status == 1: # Compute tax for married file jointly
print("Left as exercise")
elif status == 2: # Compute tax for married separately
print("Left as exercise")
elif status == 3: # Compute tax for head of household
print("Left as exercise")
else:
print("Error: invalid status")
sys.exit()
# Display the result
print("Tax is", format(tax, ".2f"))
(0-single filer, 1-married jointly, 2-married separately, 3-head of household) Enter the filing status: 0 Enter the taxable income: 666666 Tax is 211016.60
age = 18
age > 15
True
not (age > 15)
False
# Receive an input
number = eval(input("Enter an integer: "))
if number % 2 == 0 and number % 3 == 0:
print(number, "is divisible by 2 and 3")
if number % 2 == 0 or number % 3 == 0:
print(number, "is divisible by 2 or 3")
if (number % 2 == 0 or number % 3 == 0) and \
not (number % 2 == 0 and number % 3 == 0):
print(number, "divisible by 2 or 3, but not both")
Enter an integer: 8 8 is divisible by 2 or 3 8 divisible by 2 or 3, but not both
year = eval(input("Enter a year: "))
# Check if the year is a leap year
isLeapYear = (year % 4 == 0 and year % 100 != 0) or \
(year % 400 == 0)
# Display the result
print(year, "is a leap year?", isLeapYear)
Enter a year: 2024 2024 is a leap year? True
import random
# Generate a lottery
lottery = random.randint(0, 99)
# Prompt the user to enter a guess
guess = eval(input("Enter your lottery pick (two digits): "))
# Get digits from lottery
lotteryDigit1 = lottery // 10
lotteryDigit2 = lottery % 10
# Get digits from guess
guessDigit1 = guess // 10
guessDigit2 = guess % 10
print("The lottery number is", lottery)
# Check the guess
if guess == lottery:
print("Exact match: you win $10,000")
elif (guessDigit2 == lotteryDigit1 and \
guessDigit1 == lotteryDigit2):
print("Match all digits: you win $3,000")
elif (guessDigit1 == lotteryDigit1
or guessDigit1 == lotteryDigit2
or guessDigit2 == lotteryDigit1
or guessDigit2 == lotteryDigit2):
print("Match one digit: you win $1,000")
else:
print("Sorry, no match")
Enter your lottery pick (two digits): 67 The lottery number is 57 Match one digit: you win $1,000
25 // 10
2
25 % 10
5
a = 4
d = 88 if a > 4 else 9
d
9
c = a + -d
c
-5
1 - 2 - 3
-4
(1 - 2) - 3
-4
1 - (2 - 3)
2
2 ** 3 ** 4
2417851639229258349412352
2 ** (3 ** 4)
2417851639229258349412352
(2 ** 3) ** 4
4096
import turtle
x1, y1 = eval(input("Enter the center of a circle x, y: "))
radius = eval(input("Enter the radius of the circle: "))
x2, y2 = eval(input("Enter a point x, y: "))
# Draw the circle
turtle.penup() # Pull the pen up
turtle.goto(x1, y1 - radius)
turtle.pendown() # Pull the pen down
turtle.circle(radius)
# Draw the point
turtle.penup() # Pull the pen up
turtle.goto(x2, y2)
turtle.pendown() # Pull the pen down
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("red")
turtle.circle(3)
turtle.end_fill() # Fill the shape
# Display the status
turtle.penup() # Pull the pen up
turtle.goto(x1 - 70, y1 - radius - 20)
turtle.pendown()
d = ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) ** 0.5
if d <= radius:
turtle.write("The point is inside the circle")
else:
turtle.write("The point is outside the circle")
turtle.hideturtle()
turtle.done()
Enter the center of a circle x, y: 60,60 Enter the radius of the circle: 30 Enter a point x, y: 120,120
## CHAPTER 5
count = 0
while count < 5:
print("Programming is fun!", "count =", count)
count = count + 1
Programming is fun! count = 0 Programming is fun! count = 1 Programming is fun! count = 2 Programming is fun! count = 3 Programming is fun! count = 4
# 1+2+3 + ...... + 100000
count = 0
sum = 0
while count < 1_000_000_000:
count = count + 1
sum = sum + count
print(sum)
500000000500000000
import random
import time
correctCount = 0 # Count the number of correct answers
count = 0 # Count the number of questions
NUMBER_OF_QUESTIONS = 3 # Constant
startTime = time.time() # Get start time
while count < NUMBER_OF_QUESTIONS:
# 1. Generate two random single-digit integers
number1 = random.randint(0, 9)
number2 = random.randint(0, 9)
# 2. If number1 < number2, swap number1 with number2
if number1 < number2:
number1, number2 = number2, number1
# 3. Prompt the student to answer "what is number1 - number2?"
answer = eval(input("What is " + str(number1) + " - " +
str(number2) + "? "))
# 5. Grade the answer and display the result
if number1 - number2 == answer:
print("You are correct!")
correctCount += 1
else:
print("Your answer is wrong.\n", number1, "-",
number2, "should be", (number1 - number2))
# Increase the count
count += 1
endTime = time.time() # Get end time
testTime = int(endTime - startTime) # Get test time
print("Correct count is", correctCount, "out of",
NUMBER_OF_QUESTIONS, "\nTest time is", testTime, "seconds")
What is 5 - 3? 2 You are correct! What is 8 - 6? 2 You are correct! What is 9 - 0? 2 Your answer is wrong. 9 - 0 should be 9 Correct count is 2 out of 3 Test time is 8 seconds
import random
# Generate a random number to be guessed
number = random.randint(1, 100)
print("Guess a magic number between 0 and 100")
# Prompt the user to guess the number
guess = eval(input("Enter your guess: "))
if guess == number:
print("Yes, the number is " + str(number))
elif guess > number:
print("Your guess is too high")
else:
print("Your guess is too low")
Guess a magic number between 0 and 100 Enter your guess: 55 Your guess is too low
import random
# Generate a random number to be guessed
number = random.randint(1, 100)
print("Guess a magic number between 0 and 100")
guess = -1
while guess != number:
# Prompt the user to guess the number
guess = eval(input("Enter your guess: "))
if guess == number:
print("Yes, the number is", number)
elif guess > number:
print("Your guess is too high")
else:
print("Your guess is too low")
Guess a magic number between 0 and 100 Enter your guess: 50 Your guess is too low Enter your guess: 75 Your guess is too high Enter your guess: 62 Yes, the number is 62
data = eval(input("Enter an integer (the input exits " +
"if the input is 0): "))
# Keep reading data until the input is 0
sum = 0
while data != 0:
sum += data
data = eval(input("Enter an integer (the input exits " +
"if the input is 0): "))
print("The sum is", sum)
Enter an integer (the input exits if the input is 0): 6 Enter an integer (the input exits if the input is 0): 6 Enter an integer (the input exits if the input is 0): 6 Enter an integer (the input exits if the input is 0): 7 Enter an integer (the input exits if the input is 0): 7 Enter an integer (the input exits if the input is 0): 7 Enter an integer (the input exits if the input is 0): 0 The sum is 39