1. (easy) Make a loop that will print every whole number from 1 until 100.
for loop in range(1,101):
print(loop)
Anyway, this works by saying that for every item in a list from 1 until 100, print that item from the list.
2. (easy) try to get someone to input a whole number between 0 and 9 and keep asking until you get one.
n = int(input("Please input a number between 0 and 9: "))
while (n <= 0 or n >= 9):
n = int(input("Please input a number between 0 and 9: "))
print("Thanks you inputted a valid number")
It then says that while n is less than or equal to zero and greater than or equal to nine we want to keep the code going so that we can get a valid number.
If the number inputted is accurate to start with, the code inside this loop won't run and the user will be told they've inputted a correct number.
3. (medium) make code for the classic fizz-buzz game (this is what you would be asked to do if you wanted to become a software engineer, they like to ask it at interviews - or so I've been told). Essentially, take your code from question 1 and if the number is divisible by three to give a whole number print fizz, if it is divisible by five to give a whole number print buzz, and if it is divisible by both print "fizzbuzz" (hint, the % modulo symbol helps you find the remainder: 5%3=2, 1%3=1, 6%3=0.
fizz = 3
buzz = 5
for loop in range(1,101):
if (loop % fizz == 0 and loop % buzz == 0):
print("fizzbuzz")
elif (loop % fizz == 0):
print("fizz")
elif (loop % buzz == 0):
print("buzz")
else:
print(loop)
3. (medium) make a "guess my number" game. Get the computer to generate a random number between 1 to 100. Then ask the user to input a number, if the number is greater than compNumber then tell them that and if it's less than or equal to then tell them that. They have 10 attempts to guess the right number otherwise they lose and the number is revealed to them.
from random import randint
myNum = randint(1,101)
print("I've thought of a number between 1 and 100, can you guess it?")
for guesses in range(3,0,-1):
guess = int(input())
if guess > myNum:
print("Lower, you have "+guesses+" guesses left")
if guess < myNum:
print("Higher, you have "+guess+" guesses left")
else:
print("You are correct!")
break
4. (hard) make a rock, paper scissors game (user v computer)
from random import randint
choices = ["rock","paper","scissors"]
n = True
while n:
choice = choices[randint(0,2)]
n = False
userChoice = input("Choose rock, paper or scissors: ")
if userChoice.lower() == "rock":
if choice == "paper":
print("You win!")
elif choice == "scissors":
print("You lose!")
else:
print("We drew!")
if userChoice.lower() == "paper":
if choice == "rock":
print("You win!")
elif choice == "scissors":
print("You lose!")
else:
print("We drew!")
if userChoice.lower() == "scissors":
if choice == "rock":
print("You lose!")
elif choice == "paper":
print("You win!")
else:
print("We drew!")
else:
n = True
if n == False:
userChoice = input("Do you want to quit y/n?")
if userChoice.lower == "y":
n = True
else:
n = False
Edit Fixed the errors in the last solution.