In: Computer Science
Python Programming Please!
#Name:
#Date:
#Random number, loop while true
#ask user for number. Check to see if the value is a number between 1 and 10
#if number is too high or too low, tell user, if they guessed it break out of loop
Display "Welcome to my Guess the number program!"
random mynumber
count=1
while True
try
Display "Guess a number between 1 and 10"
Get guess
while guess<1 or guess>10
Display "Guess a number between 1 and 10"
Get guess
except
Display "numbers only"
continue
if (guess<mynumber)
Display "Too low"
count=count+1
else if (guess>mynumber)
Display "Too high"
count=count+1
else if (guess==mynumber)
Display "You guessed it in "+ count + " attempts"
When you run the program the result should look like the following:
Welcome to my Guess the number program!
Please guess a number between 1 and 10: a
Numbers only!
Please guess a number between 1 and 10: -3
Please guess a number between 1 and 10: 4
Too low
Please guess a number between 1 and 10: 5
Too low
Please guess a number between 1 and 10: 6
You guessed it! It took you 3 attempts
Be sure to submit your assignment.
Course Project (Weeks 4-7)
Code:
import random
mynumber=random.randint(1,10)
#getting random number between 1 to 10
count=1
#initialized count to 10
while(True):
#Infinet while loop
try:
guess=int(input("Guess a number between 1 and 10 : "))
#here we take input
while(guess<1 or guess>10):
#we ask the user untill he enter number between 1 and 10
guess=int(input("Guess a number between 1 and 10 : "))
if(guess<mynumber):
#if the guessed number is less than the random number
#we print too low
print("Too low")
count+=1
#Here we increase the count
elif(guess>mynumber):
print("Too high")
#if the guessed number is greater than the random number
#we print too high
count+=1
#Here we increase the count
elif(guess==mynumber):
print("You guessed it in "+str(count)+" attempts")
#if the gussed number is equals to the number
#then we break the loop
break
except:
#if any exeption occured we execute the except block
print("numbers only")
Output:
Indentation: