Practice Exercise 6
# Problem Statement:-
# Generate a random integer from a to b. You and your friend have to guess a number
# between two numbers a and b. a and b are inputs taken from the user. Your friend is
# player 1 and plays first. He will have to keep choosing the number and your program
# must tell whether the number is greater than the actual number or less than the actual
# number. Log the number of trials it took your friend to arrive at the number.
# You play the same game and then the person with minimum number of trials wins!
# Randomly generate a number after taking a and b as input and don’t show that to the user.
# Input:
# Enter the value of a
# 4
# Enter the value of b
# 13
# Output:
# Player1 :
# Please guess the number between 4 and 13
# 5
# Wrong guess a greater number again
# 8
# Wrong guess a smaller number again
# 6
#Correct you took 3 trials to guess the number
# Player 2:
# Correct you took 7 trials to guess the number
# Player 1 wins!
'''
Author : Aryan Sonone
Date : 11/11/2020
Purpose : Practicing
'''
import random
import time
a = int(input("Dear User please Enter The Value of a :\n"))
b = int(input("Dear User please Enter The Value of b :\n"))
while(True):
player1or2 = int(input("Please Enter Who Is Playing?\n"
"Press 1 for Player 1 And Press 2 For Player 2\n"))
if player1or2==1:
#Generate 5 random numbers between a and b
ranlist = random.sample(range(a, b), 5)
# random.choice picks a random number from list
guessnum = random.choice(ranlist)
# print(guessnum)
# Taking input from user to guess guessnum
n = guessnum
numguess1 = 1
while (True):
guess_number = int(input("Guess the number :\n"))
if guess_number < guessnum:
print("You enter less number please input greater number.\n")
numguess1 = numguess1 + 1
elif guess_number > guessnum:
print("You enter greater number please input smaller number.\n ")
numguess1 = numguess1 + 1
# input is equal to guessnum and won the game
else:
print("You won\n")
print(f"You took {numguess1} Guesses to Guess The Number")
number_of_guesses = numguess1 + 1
f = open("Player 1 Score.txt", "r+")
f.read()
f.write(
f"\nRecently A Score Was Added to the score list.\n"
f"Score is {numguess1}")
localtime = time.asctime(time.localtime(time.time()))
f.write("\nThe time at which the score was added is:\n")
f.write(localtime)
break
# Same is repeated for the player 2
if player1or2==2:
# Generate 5 random numbers between a and b
ranlist = random.sample(range(a, b), 5)
# random.choice picks a random number from list
guessnum = random.choice(ranlist)
# print(guessnum)
n = guessnum
numguess2= 1
while (True):
guess_number = int(input("Guess the number :\n"))
if guess_number < guessnum:
print("You enter less number please input greater number.\n")
numguess2= numguess2+ 1
elif guess_number > guessnum:
print("You enter greater number please input smaller number.\n ")
numguess2= numguess2+ 1
else:
print("You won\n")
print(f"You took {numguess} Guesses to Guess The Number")
break
number_of_guesses = numguess2+ 1
# Appending the score to Player 2 Score .txt file
f = open("Player 2 Score.txt", "r+")
f.read()
f.write(
f"\nRecently A Score Was Added to the score list.\n"
f"Score is {number_of_guesses}")
localtime = time.asctime(time.localtime(time.time()))
f.write("\nThe time at which the score was added is:\n")
f.write(localtime)
print("Press q to quit and c to continue")
user_choice2 = ""
while (user_choice2 != "c" and user_choice2 != "q"):
user_choice2 = input()
if user_choice2 == "q":
exit()
elif user_choice2 == "c":
continue
Comments
Post a Comment