Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,829 members, 7,810,188 topics. Date: Friday, 26 April 2024 at 10:50 PM

Help With This Python Algorithm.... - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help With This Python Algorithm.... (1274 Views)

Python Masters, Kindly Help Me Rectify This Python Problem. / Please Review This Python Blog By A Nigerian. / Pls Programmers Help With Similarities Of Pseudocode & Algorithm (2) (3) (4)

(1) (Reply) (Go Down)

Help With This Python Algorithm.... by emilykel(m): 10:59am On Sep 26, 2015
hello guys and my fellow python gurus..
I wrote this simple guess game in python using a while loop and random module...
the game asks you to guess what it's thinking of then you enter the number,however when you make a wrong guess, it picks another random number..
the issue is, since I wrote it, Av won only about 3 times out of about 300 guesses..I want you guys to bring ideas on how to make the game more winnable because the chances of making a guess are only 20...

#-*-coding:utf8;-*-
#qpy:3
#qpy:console

import random
print (""wink
print("this is a guessing game,you have [ 20 ] attempts"wink
print("this is v.3 in attempt to make the game easy.. "wink
print(""wink
print(" LETS PLAY THE GAME"wink
print (""wink
number=random.choice(range(1,150))
Max = 150
minn =1
failed = 0
left = 20
while True:

guess=input("GUESS THE NUMBER AM THINKING OF/>> "wink
if guess.isalpha():
print (" sorry,you must enter a digit "wink
print(""wink
else:
if int(guess) == number:
print(""wink
print("###################################"wink
print("# CONGRATULATIONS #"wink
print("# YOU WON #"wink
print("###################################"wink

break

elif int(guess) > Max:
print ("your guess is a bit too high...."wink
print(""wink

elif int(guess) < minn:
print (" your guess is a little too low..."wink
print(""wink

elif failed == 20:
print (" you have exhausted your chances..."wink
print(""wink
print ("##################################"wink
print ("# GAME OVER #"wink
print("##################################"wink
print(""wink
print(" [ game by VITRIOL ] "wink
break

elif left==1:
print("wrong guess, try again .. "wink
failed+=1
left-=1
print("your last attempt... "wink
print(""wink

else:
print("wrong guess, try again .. "wink
failed+=1
left-=1
print (left," attempt(s) left.."wink
print (""wink

this is the python code..

Re: Help With This Python Algorithm.... by KelvinMega(m): 1:28pm On Sep 26, 2015
reduce the range of the numbers. the probability of guessing a number between 1 and150 is quite low.(1- 30 should be fairly easy) then you should tell your players the range to guess from
Re: Help With This Python Algorithm.... by emilykel(m): 2:12pm On Sep 26, 2015
I know, but I don't wanna reduce the range, am thinking about making 4 picks from behind the scenes so that each guess is checked against the picks for at least a win
Re: Help With This Python Algorithm.... by Nobody: 5:35pm On Sep 26, 2015
emilykel:
I know, but I don't wanna reduce the range, am thinking about making 4 picks from behind the scenes so that each guess is checked against the picks for at least a win

you either reduce the range or you increase the no of failed attempts. Why did you use Random instead of importing randint from Random, since you are using random integers

Also i think you should have used
while guess != 0
Re: Help With This Python Algorithm.... by emilykel(m): 6:23pm On Sep 26, 2015
randint() and random.choice(range()) are the same, besides Av used randint before...
I also see no reason to use
while guess!=0
Re: Help With This Python Algorithm.... by Greatfes17: 11:31pm On Sep 27, 2015
You may try allowing the user enter more than one number and then check if any if them matches the guess.

I have also similar game written some months back but the guess is between 1 to 20.

I don't know how the [code] tag works here so i recommend uploading the file you need help on some other time. my opinion though
Re: Help With This Python Algorithm.... by Austinoski(m): 11:57pm On Sep 28, 2015
use a "for" loop and a list to get four or more random numbers from the Computer and then check the players answer to see if it matches any of those numbers in the list, if true then the player won

guess_list = []
for i in range(4):
guess = random.randrange(1,1150)
guess_list.append(guess)

answer = int(raw_input("Enter Guessed number"wink)
if (answer in guess_list):
player won
else:
player lost
Re: Help With This Python Algorithm.... by silento(m): 12:30am On Sep 29, 2015
this is a thing I did while am new to python
solution....
decide random number outside the while loop
ex:
number = random.choice(range(200))
run = 0
while run !=0:
code ...
if choice > number :
print number is lower
if choice is < number :
print number is. higher

if choice == number:
print yes

run = 1
Re: Help With This Python Algorithm.... by matthew80(m): 9:42am On Sep 30, 2015
Even in real life, a guess out of 150 is too slim, let alone in computing. Dunno why you're sticking to that range though. Meanwhile, you can allow players to input 4 or 5 numbers and check if any matches, or let the computer think of 4 to 5 numbers and check if the single input matches any.
Re: Help With This Python Algorithm.... by sleepingdemon: 2:44pm On Sep 30, 2015
basbone:


you either reduce the range or you increase the no of failed attempts. Why did you use Random instead of importing randint from Random, since you are using random integers

Also i think you should have used
while guess != 0

(1) (Reply)

Devs: Here Is A Free Idea For You / My Harddisk Got Corrupted.I Need Help Recovering My Files.kindly Help / Contact Me For Payment Integration For Prestashop

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 26
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.