In: Computer Science
Create a PYTHON program that lets a user guess a number from 1 to 1,000:
-i used random.randint(1,1000)
-must give hints like (pick a lower/higher number) which i already did
-tell the user when he has repeated a number (help)
-the game only ends when you guess the number (help)
-you loose $50 every failed attempt (done)
****I did it as a while loop
-
RAW CODE
import random
random_number = random.randint(1,1000)
print("Welcome to random number game")
guessed_values = [] ### List is made to keep track of the guessed values
while(1): ### Infinite loop, stops only when breaked
number = int(input("Pick a number between 1-1000: "))
if number in guessed_values: ### If number in the list, then tell that number is repeated.
print("You are repeating this number, you aleardy tried it")
guessed_values.append(number)
elif number == random_number:
print("Wow! You guessed it Right")
print("Total_lose = ${}".format(len(guessed_values)*50))
break #### Game ends when guessed right
elif number > random_number:
print("Pick a lower number")
guessed_values.append(number)
else:
print("Pick a higher number")
guessed_values.append(number)
SCREENSHOTS (CODE WITH OUTPUT)
CASE 1 (WITHOUT REPETITION)
CASE 2 (WITH REPETITION)
RAW CODE (NOT FOLLOWING HINTS)
import random
random_number = random.randint(1,1000)
print("Welcome to random number game")
guessed_values = [] ### List is made to keep track of the guessed values
count = 0
while(1): ### Infinite loop, stops only when breaked
number = int(input("Pick a number between 1-1000: "))
if number in guessed_values: ### If number in the list, then tell that number is repeated.
guessed_values.append(number)
count += 1
if count == 1:
print("Warning! Repeated value ($10 will be deducted from next attempt for every repeated value)")
if count > 1:
print("Repeated Value, $10 deducted")
elif number == random_number:
print("Wow! You guessed it Right")
print("Total_lose = ${}".format(len(guessed_values)*50 + (count-1) * 10))
break #### Game ends when guessed right
elif number > random_number:
print("Pick a lower number")
guessed_values.append(number)
else:
print("Pick a higher number")
guessed_values.append(number)
SCREENSHOTS (CODE WITH OUTPUT)
NOTE:- In this $10 is deducted every time on repeating value after the first warning. Eg:- If 2 times repeated after warning, then $20 panelty added in last.
##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####