Python Problem 6 | Python Tutorials For Absolute Beginners In Hindi #113

preview_player
Показать описание


Best Hindi Videos For Learning Programming:

Follow Me On Social Media
Рекомендации по теме
Комментарии
Автор

# Most easiest challenge-

import random

user_num1 = int(input("Enter the first number: "))
user_num2 = int(input("Enter the second number: "))

number = random.randint(user_num1, user_num2)
t = 1

while True:
inp = int(input("Guess your number: "))
if inp > number:
t += 1
print("Please decrease this number!")
elif inp < number:
t += 1
print("Please increase the number!")
elif number == inp:
print("Well done, its correct!")
break

print(f"You took {t} trials to reach {number}")

rohanbeast
Автор

def player1(num, name1):
print("PLAYER 1 :\n")
i=0
while i<=10:
g = int(input("Enter Your Guess :"))
if g<num:
print("WORNG! enter a Larger Number!")
elif g>num:
print("WORNG! enter a Smaller Number!")
else:
print(f"----RIGHT!---- \nNow {name2}'s Turn ")
return i
i+=1
if i>=10:
print("---Your turn is Over!---")
return i


def player2(num, name2):
print("PLAYER 2 :\n")
i=0
while i<=10:
g = int(input("Enter Your Guess :"))
if g<num:
print("WORNG! enter a Larger Number!")
elif g>num:
print("WORNG! enter a Smaller Number!")
else:
print("----RIGHT!----")
return i
i+=1
if i>=10:
print("---Your turn is Over!---")
return i







if __name__ == "__main__":
import random
name1= input("Enter Name of Player 1 :")
name2= input("Enter Name of Player 2 :")
a= int(input("Enter Starting Point :"))
b= int(input("Enter Ending Point :"))
num = random.randrange(a, b)
p1= player1(num, name1)
p2= player2(num, name2)
if p1 <p2:
print(f"{name1} took {p1} Chances \n{name2} took {p2} Chances \n
elif p1>p2:
print(f"{name1} took {p1} Chances \n{name2} took {p2} Chances \n
else:
print(f"{name1} took {p1} Chances \n{name2} took {p2} Chances \n Tie's

ManojKumar-itri
Автор

Challenge Accepted!
Edit:

'''
Author: Garvit Singh
Date: 19 February 2022
Purpose: For Practice Problem from CodeWithHarry Channel
'''

import random

a = int(input("Enter the number a: "))
b = int(input("Enter the number b: "))

rnd = random.randint(a, b)
trails_p1 = 0
trails_p2 = 0

while True:
while True:
print("Player 1:\n")
num_p1 = int(input(f"Guess a number between {a} and {b}: "))

if num_p1 > rnd:
trails_p1 += 1
print("Wrong guess a smaller number again")

elif num_p1 < rnd:
trails_p1 += 1
print("Wrong guess a greater number again")

elif num_p1 == rnd:
trails_p1 += 1
print(f"Correct you took {trails_p1} to guess the number")
break

while True:
print("Player 2:\n")
num_p2 = int(input(f"Guess a number between {a} and {b}: "))

if num_p2 > rnd:
trails_p2 += 1
print("Wrong guess a smaller number again")

elif num_p2 < rnd:
trails_p2 += 1
print("Wrong guess a greater number again")

elif num_p2 == rnd:
trails_p2 += 1
print(f"Correct you took {trails_p2} to guess the number!")
break
break


if trails_p1 > trails_p2:
print("Player 2 wins!")

elif trails_p2 > trails_p1:
print("Player 1 wins!")

darkalpha
Автор

import random
def play_game(a, b):
random_number = random.randint(a, b)

#number of trials is 0 at beginning
t = 0
while True:
print("Choose any number between", a, "and", b)
guess_num = int(input())

if guess_num < random_number:
print("You guessed smaller number.")
t += 1
elif guess_num > random_number:
print("You guessed greater number.")
t += 1

else:
print("Correct. You guessed in ", t+1, "attempts")
return t


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Player 1 turn")
player1 = play_game(num1, num2)
print("Player 2 turn")
player2 = play_game(num1, num2)

if player1 < player2:
print("Player 1 wins.")
elif player1 > player2:
print("Player 2 wins")
else:
print("Draw")

tutuber
Автор

I have done almost all the error handling too...

import random

# checks that the given input is integer
def true_choice():
while True:
try:
choice = int(input())
break
except ValueError:
print("Enter only interger values")
return choice

# function for guessing the number
def guess(number):
trials=1
choice_player = true_choice()

while choice_player!=number:
trials +=1
if choice_player<number:
print("Wrong guess!!! a greater number again")
choice_player = true_choice()
else:
print("Wrong guess!!! a smaller number again")
choice_player = true_choice()

print(f"Correct you took {trials} trials to guess the number {number}\n")
return trials


if __name__ == "__main__":
try:
a = int(input("Enter the value of a : "))
b = int(input("Enter the value of b : "))
except ValueError:
print("Please choose integer values")
exit()

rand_number1 = random.randint(a, b)
print("Player 1 : ")
print(f"Please guess the number between {a} and {b}")
trial1 = guess(rand_number1)

rand_number2 = random.randint(a, b)
print("Player 2 : ")
print(f"Please guess the number between {a} and {b}")
trial2 = guess(rand_number2)

if (trial1<trial2):
1
elif (trial1>trial2):
2
else:

debanjansinha
Автор

import random
a=int(input("Enter min number of the range: "))
b=int(input("Enter max number of the range: "))
n=int(input("Enter the numbers of player: "))
win = []
for j in range(1, n+1):
print(f"PLAYER-{j}")
c=random.randrange(a, b)

for i in range(1, 100):

ch = int(input("enter your choice: "))
if (ch==c):
print("You guess it
print(f"PLAYER-{j} guess the number in {i} chances\n")
win.append(i)
break
elif(ch<c):
print("Wrong guess.Yous guess is smaller then actual:")
i=i+1
continue
elif(ch>c):
print("Wrong Choice. Your choice is greater then actual number!!")
i=i+1
continue
print(win)
min = min(win)
idx=win.index(min)

r= len(win)
for i in range(n):
if win[i] == min:
print(f"Player-{i+1} wins")
else:
continue

vasim_delhi
Автор

from random import randint as rand
tri = 0
r1 = int(input("Enter number starts random number generatong range: "))
r2 = int(input("Enter number ends random number generatong range: "))
def main(player):
ans = rand(r1, r2)
print(str(player)+":")
while 1:
global tri
chs = int(input("Guess the number: "))
if chs == ans: break
elif chs > ans: print("Wrong guess a smaller number this time\n")
elif chs < ans: print("Wrong guess a greater number this time\n")
tri+=1
print(f"{player} done it in", tri+1)
return tri
main("player1")
tri1, tri = tri, 0
main("player2")
tri2, tri = tri, 0
if tri1 < tri2: print("Player1 wins")
elif tri1 > tri2: print("Player2 wins")
elif tri1 == tri2:
print("These two are our gems who give us equal scores means tie")

topicalclassteacher
Автор

import random
a=int(input("Enter the starting number: "))
b=int(input("Enter the ending number: "))
rnumber = random.randint(a, b)
def trials(rnumber):
print("Please enter your first guess")
counter = 1
while(True):
guess = int(input())
if guess==rnumber:
print(f"Correct, you took {counter} tries to get the number")
break
elif guess<rnumber:
print("Wrong, guess a greater number")
counter+=1
elif guess>rnumber:
print("Wrong, guess a smaller number")
counter+=1
return counter
print("Player1")
p1 = trials(rnumber)
print("Player2")
p2 =trials(rnumber)
if p1>p2:
print("Player2 wins!")
elif p2>p1:
print("Player1 wins!")
else:
print("It's a draw..")

kaustubh
Автор

import random

def player():
i=1
while True:
g=int(input("Please guess the number "))

if g==r:
print(f"You guessed the right number.\nyou took {i} chances to guess the number")
break
elif g>r:print("Please give another number.This is greater than the original one")
elif g<r:print("Please give another number.This is smaller than the original one")
i=i+1
return i-1

while True:

while True:
a=int(input("Please enter the value of a\t"))
b=int(input("PLease enter the value of b\t"))
if a>b:
r=random.randint(b+1, a-1)
break
elif a<b:
r=random.randint(a+1, b-1)
break
elif a==b:print("The number should not be equal. Please try again")
print()

print("Player 1:")
p1=player()
print("Player 2:")
p2=player()
if p1>p2:print("Player 2 wins")
elif p2>p1:print("Player 1 wins")
elif p2==p1:print("No one wins Game is draw")
c=input("To play again press y else n")
if c=="y": pass
elif c=="n":break
else:print("PLease give valid input")

tanmaydaga
Автор

import random
inp1=int(input("Enter 1st no: "))
inp2=int(input("Enter 2nd no: "))
answer = random.choice(range(inp1, inp2))
print(answer)
print("Play Player 1 : ")
c=0
c1=0
while 1:
inp3=int(input("Enter Your no : "))
if inp3>answer :
print("Enter Less no ")
elif inp3<answer :
print("Enter greater no ")
else:
print(f"Congratulation !!! No of trials = {c}")
break
c+=1
print("Play Player 2 : ")
while 1:
inp3=int(input("Enter Your no : "))
if inp3>answer :
print("Enter Less no ")
elif inp3<answer :
print("Enter greater no ")
else:
print(f"Congratulation !!! No of trials = {c1}")
break
c1+=1

if c1>c:
print("Plaer 1 Win The game!!")
elif c1<c:
print("Plaer 2 Win The game!!")
else:
print("Draw!!")

AnubhavTechnicalTechstar
Автор

import random

a = int(input("Enter the value of a: "))
b = int(input("Enter the value of b: "))

actual_number = random.randint(a, b)

def guessGame(player):
p1_count = 0
print(player)
while True:
p1_count += 1
guess_num1 = int(input("Please enter a number between " + str(a) + " and " + str(b) + ": "))
if guess_num1 == actual_number:
print("Correct, You have taken " + str(p1_count) + " chance to guess the number!")
break
elif guess_num1 > actual_number:
print("You entered a greater number than the actual number, Try again!")
elif guess_num1 < actual_number:
print("You entered a lower number than the actual number, Try again!")

if __name__ == "__main__":
player1 = "Player 1"
player2 = "Player 2"

guessGame(player1)
guessGame(player2)

import os

os.system("pause")

'''
Author: Suraj Singh
Purpose: Python Problem Solving
Problem Credit: Owner of CodeWithHarry Channel (Harry)
'''

SurajSingh-mfvz
Автор

import random
a=int(input())
b=int(input())
l=[i for i in range(a, b)]
g1=random.choice(l)
print("Player1 are you ready!!!")
t1=0
c1=0
while(t1!=g1):
t1=int(input('Player1 guess the number between {0} and {1}'.format(a, b)))
c1+=1
if(t1>g1):
print("Number is quite smaller")
elif t1==g1:
print("You got it after {0} attempts".format(c1))
else:
print("Number is quite greater")
print("Player2 are you ready!!!")
g2=random.choice(l)
t2=0
c2=0
while(t2!=g2):
t2=int(input('Player2 guess the number between {0} and {1}'.format(a, b)))
c2+=1
if(t2>g2):
print("Number is quite smaller")
elif t2==g2:
print("You got it after {0} attempts".format(c2))
else:
print("Number is quite greater")
if c1>c2:
print("Player 2 Won this game as he took lesser number of attempts")
elif c1==c2:
print("Draw")
else:
print("Player 1 won this game as he took lesser number of attempts")

anuragmishra
Автор

import random

a = int(input("Enter the number from wher you want to start\n"))
b = int(input("Enter the number from wher you want to end\n"))
random_number1 = random.randint(a, b) #Slecting a number between a and b for Player1
random_number2 = random.randint(a, b) #Slecting a number between a and b for Player2
i=0 #Chance counter for Player1
j=0 #Chance counter for Player2

#This loop is for Player1
print("Player1:")
while True:
choice1 = int(input("Enter your guess\n")) #Choosing a number by Player1
if choice1>random_number1:
i += 1
print("Wrong Guess!You guess a greater number")
if choice1<random_number1:
i += 1
print("Wrong Guess!You guess a smaller number")
if choice1==random_number1:
i += 1
print("Correct Guess!")
print(f"You take {i} chance") #Adds the chance taken by Pyayer1
break #Breaking the function if user chooses correct number

#This loop is for Player2
print("Player2:")
while True:
choice2 = int(input("Enter your guess\n")) #Choosing a number by Player2
if choice2>random_number2:
j += 1
print("Wrong Guess!You guess a greater number")
if choice2<random_number2:
j += 1
print("Wrong Guess!You guess a smaller number")
if choice2==random_number2:
j += 1
print("Correct Guess!")
print(f"You take {j} chance") #Adds the chance taken by Pyayer1
break #Breaking the function if user chooses correct number

#Compairing the chance taken by the Players.That player wins who take less chances
if i<j:
print("Player1 Wins!")
else:
print("Player2 Wins!")

priyankagarg
Автор

from random import randint

def choose(no):
count=1
while True:
chooseNo = int(input(f"Please guess a no in between {a} and {b}"))
if(chooseNo>no):
print("wrong guess a gretter no ")
elif chooseNo<no:
print("wrong guess a smaller no again")
else:
print(f"you took {count} chances ")
break
count+=1
return count
if __name__ == "__main__":

a=int(input("Enter the value of a"))
b=int(input("Enter the value of b"))
no=randint(a, b)
player1, player2 =0, 0

print("player1:")
player1=choose(no)
print("palyer2:")
player2=choose(no)
if player1>player2:
print("player 2 win")
elif player1<player2:
print("player 1 win")
else:
print("draw match")

alokbhowmik
Автор

# Python problem 6
import random
a = int(input("Enter the value of a:"))
b = int(input("Enter the value of b:"))
count_p1 = 0
count_p2 = 0
mylist = []
# This for loop is used for make the list of the number b/w a and b
for i in range(a, b+1):
mylist.append(i)
print(mylist)

# For choosse the random number
ran_num = random.choice(mylist)

# For player 1
print(f"""
Player 1:
Please guess the number between {a} and {b}
""")
# This loop is repeat until player 1 finish the game
while(True):
# Count the number of turns of palyer of p1
count_p1 = count_p1 + 1
p1_value = int(input("Enter any number:\n"))
if p1_value == ran_num:
print(f"Correct, You took {count_p1} trials to guess the number!\n")
break
elif p1_value > ran_num:
print("Wrong, guess a smaller number!\n")
else:
print("Wrong, guess a greater number!\n")

# For player 2
print(f"""
Player 2:
Please guess the number between {a} and {b}
""")
# This loop is repeat until player 2 finish the game
while(True):
# Count the number of turns taken by p2
count_p2 = count_p2 + 1
p1_value = int(input("Enter any number:\n"))
if p1_value == ran_num:
print(f"Correct, You took {count_p2} trials to guess the number!\n")
break
elif p1_value > ran_num:
print("Wrong, guess a smaller number!\n")
else:
print("Wrong, guess a greater number!\n")


# These if else conditions tell us who wins the game or tie
if count_p1 == count_p2:
print(f"The game is tie on {count_p1}")
elif count_p1 > count_p2:
print(f"Player 2 WINS The match!\nBy the lead of {count_p1 - count_p2}")
elif count_p2 > count_p1:
print(f"Player 1 WINS The match!\nBy the lead of {count_p2 - count_p1}")

jayeshkaushik
Автор

print("Welcome to guess the number game.")
print("You have to guess the number. The player who guesses it in lesser time wins.")
target=17
guesses1=0
guesses2=0
while(True):
player1=int(input("Player 1 type the number.\n"))
guesses1=guesses1+1
if player1==target:
print(f"Correct answer. You have guessed in {guesses1} chance.")
break
elif player1<target:
print("Choose a bigger number.")

elif player1>target:
print("Choose a smaller number.")
while(True):
player2=int(input("Player 2 guess the number.\n"))
guesses2 = guesses2 + 1
if player2 == target:
print(f"Correct answer. You have guessed in {guesses2} chance.")
break
elif player2 < target:
print("Choose a bigger number.")

elif player2 > target:
print("Choose a smaller number.")
while(True):
if guesses1<guesses2:
print("Player 1 wins.")
elif guesses1>guesses2:
print("Player 2 wins.")
elif guesses1==guesses2:
print("Tie.")
break

stevendna
Автор

import random
ls1=[i for i in range(4, 13)]
rChoice = int(random.choice(ls1))
print("now we playing for player 1")
i = 0
while True:
i = i + 1
pChoice = input('pls guessa number btween "4" and "13": ')
if int(pChoice) > rChoice:
print("you enter a greater number, pls enter a small number")
elif int(pChoice) < rChoice:
print("you enter a small number pls enter a greater number")
else:
print(f'you successfully guess the number and that is {pChoice}')
break


ls2=[i for i in range(4, 13)]
lChoice = int(random.choice(ls2))
print(f"now we playing for player 2 ")
p=0
while True:
p=p+1
pChoice=input('pls guessa number btween "4" and "13": ')
if int(pChoice) > lChoice:
print("you enter a greater number, pls enter a small number")
elif int(pChoice) < lChoice:
print("you enter a small number pls enter a greater number")
else:
print (f'you successfully guess the number and that is {pChoice}')
break

if i>p:
print("player 2 is the winner")
else:
print("player 1 is the winner")

talhamahamud
Автор

import random
log = {
"player1": 0,
"player2": 0
}
a = int(input("Enter the first number : "))
b = int(input("Enter the second number : "))
list1 = [c for c in range(a, b)]
Number_to_guess = random.choice(list1)
for i in range(1, 3):
print(f"PLAYER {i} : ")
while True:
guessed = int(input("Guess the number : "))
if guessed == Number_to_guess:
log[f"player{i}"]+=1
x = log[f"player{i}"]
print(f"congratulation, you guessed the right number in {x} chances")
break
else:
log[f"player{i}"]+=1
if guessed > Number_to_guess:
print("Take a too big")
elif guessed < Number_to_guess:
print("Number is small")
if log["player1"] < log["player2"]:
print("Player 1 won the match")
if log["player2"] < log["player1"]:
print("Player 2 won the match")

dnyaneshbharambe
Автор

a= int(input("enter the starting :"))
b= int(input("enter the end : "))
unknown = random.randint(a+1, b-1)
def guess(unknown):
trails = 7
attemp = 0
while trails > attemp :
print(f"you have {trails - attemp} attemps left")
guessno = int(input("guess the orginal no :"))
if guessno == unknown :
print(f"your guess is perfectly ok in the {attemp + 1} attemp only")
break
elif guessno > unknown:
print("your no is bigger than orignal no")
attemp += 1
elif guessno < unknown:
print("your no is smaller than orignal no")
attemp += 1
continue
guess(unknown)






i haven't included the player score comparison, but can code it with if else
even excluded the start and end of randint parameters to make it more challenging

mohanjangid
Автор

Done





from random import random
import random

a = int(input("Enter a number 1: "))
b = int(input("Enter a number 2: "))


num = random.randint(a, b)


turn1 = 0
turn2 = 0
p1name = input("Player 1 Enter your name: ")

while 1:
print(f"{p1name} playing... ")
guess = int(input("Please guess the number between a and b: \n"))
if guess==num:
print(f"Congrats {p1name}, You have guessed and it took you {turn1} turns.")
break
elif guess>num:
print("Please guess a smaller number\n")
turn1 +=1
elif guess<num:
print("Please enter a greater number\n")
turn1 +=1
else:
pass

p2name = input("Player 2 Enter your name: ")

while 1:
print(f"{p2name} playing... ")
guess = int(input("Please guess the number between a and b: \n"))
if guess==num:
print(f"Congrats {p2name}, You have guessed and it took you {turn2} turns.")
break
elif guess>num:
print("Please guess a smaller number\n")
turn2 +=1
elif guess<num:
print("Please enter a greater number\n")
turn2 +=1
else:
pass


if turn1<turn2:
print(f"{p1name} wins")
elif turn1>turn2:
print(f"{p2name} wins")
else:
print("Tie")

parmeetrailalwani
join shbcf.ru