Python Projects Code
Quiz
#starting the quiz with welcome message
print("Welcome to my quiz!")
#checking if the user wants to play
playing = input("Do you want to play? ")
#using if statement, so if the answer is negative the game will stop;
# the function lower will convert the letters typed by user to lower case
if playing.lower() != "yes":
quit()
#after confirmation from the user, the game will start
print("Okay! Let's play :")
#introdusing variable score in which the score for the correct answers will be kept
score = 0
#question 1
answer = input("Which flowers can be planted in spring time? ")
#using if statement to check if the answer is correct
if answer.lower() == "daffodils and tulips":
print('Correct!')
#adding 1 point to variable score, if the answer is correct
score += 1
else:
print("Incorrect!")
#question 2
answer = input("Which vegetables can we grow in spring time? ")
#using if statement to check if the answer is correct
if answer.lower() == "cucumbers":
print('Correct!')
# adding 1 point to variable score, if the answer is correct
score += 1
else:
print("Incorrect!")
#question 3
answer = input("When tomatoes can be planted? ")
#using if statement to check if the answer is correct
if answer.lower() == "april":
print('Correct!')
# adding 1 point to variable score, if the answer is correct
score += 1
else:
print("Incorrect!")
#question 4
answer = input("When potatoes can be planted? ")
#using if statement to check if the answer is correct
if answer.lower() == "may":
print('Correct!')
# adding 1 point to variable score, if the answer is correct
score += 1
else:
print("Incorrect!")
#printing statement with the score - the total number of correct answers
print("You got " + str(score) + " questions correct!")
#printing statement with the score - calculating the percentage of correct answers
print("You got " + str((score/4) * 100) + "%")
Guess Number
#importing library random, it will be used for the guessing
import random
#taking input from user
top_of_range = input("Type a number: ")
#using if statement to determine whether the input is a number
# the input will be printed with "", meaning that will be read as string
if top_of_range.isdigit():
#converting the string into integer
top_of_range = int(top_of_range)
#check if the input is bigger than zero
if top_of_range <= 0:
print("Please type a number larger than zero next time.")
quit()
else:
print("Please type a number next time.")
quit()
random_number = random.randint(0, top_of_range)
#to count how many guesses are done by the user
guesses = 0
#using while loop to prompt the user to guess the number
while True:
#counting the guesses
guesses +=1
#asking the user to enter a number
user_guess = input("Make a guess: ")
#checking if the input is a number
if user_guess.isdigit():
# converting the string into integer
user_guess = int(user_guess)
else:
print("Please type a number next time.")
#will go to the beginning of the loop
continue
#check if the user input is matching the random number
if user_guess == random_number:
print("Correct!")
#if the numbers match, the loop will end
break
#check if the number is above the random number
elif user_guess > random_number:
print("This is above the number!")
else:
print("This is below the number!")
#when the user guess the number, will print the number of guesses
print("You did", guesses, "guesses")
Rock, Paper, Scissors
import random
#introdusing variables to store the score for the user and for the computer
user_wins = 0
computer_wins = 0
#using a list to store the options for the computer
options = ["rock", "paper", "scissors"]
options[0]
#using while loop to mange user input
while True:
user_input = input("Type Rock/Paper/Scissors or Q to quit: ").lower()
#will stop the game if user enter "q"
if user_input == "q":
break
#check if the user input contains any of the keywords, we will place them in list
if user_input not in options:
#if the user input is not one of the keywords, we revert to the beginning of the loop
continue
#introducing the computer choice as random number between 0 and 2
random_number = random.randint(0, 2)
#storing the computer choice in variable
computer_choice = options[random_number]
#printing computer choice
print("Computer choose", computer_choice + ".")
#check who is the winner
if user_input == "rock" and computer_choice == "scissors":
print("You won!")
user_wins +=1
#check who is the winner
elif user_input == "paper" and computer_choice == "rock":
print("You won!")
user_wins +=1
#check who is the winner
elif user_input == "scissors" and computer_choice == "paper":
print("You won!")
user_wins +=1
#if the conditions above are not true, means that we lost, so will print the below
else:
print("You lost!")
computer_wins +=1
#will print the score
print("You won", user_wins, "times.")
print("The computer won", computer_wins, "times.")
u.