Ai Guessing Game | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Ai Guessing Game

https://code.sololearn.com/cFh2E754uqy4/?ref=app Bored at work I made a number guessing game. This time you input a number and the computer figures your number out. it zeros in pretty fast. I ran a number between 1 and 1,000,000.. it got it in 31 tries. Input A number between 0 and the size variable. make the size variable as big or small as you'd like. As always, I know, there is probably an easier way to do this. I am playing while I learn.

12th Apr 2017, 7:37 PM
LordHill
LordHill - avatar
9 Answers
+ 6
Nice code I like it. The problem here is with your statement. Code isn't really guessing the input, since you have things like if cpunum>num. So it actually knows the number. But it's still fun to see how many tries it needs
12th Apr 2017, 7:43 PM
Dinmukhamed Mailibay
Dinmukhamed Mailibay - avatar
+ 5
Quite true though. It can be named a guess in some sense. Anw it's a nice code
12th Apr 2017, 7:58 PM
Dinmukhamed Mailibay
Dinmukhamed Mailibay - avatar
+ 5
@Leaky Egg- just a suggestion... To improve performance of your AI - to make it more intelligent so that it guesses the number in fewer steps you can try updating your guess as (bigger+lower) /2 instead of random again. This way your code will work like binary search and give quick result ☺️
12th Apr 2017, 8:07 PM
Shraddha
Shraddha - avatar
+ 1
It's like the guessing game when you first start programming where it picks a random number and you guess. it tells you higher or lower. that's all that's happening here. it guesses a number, then it gets higher or lower and makes a new guess based on that. So in its way, it is guessing. it doesn't know the number, it just gets to know if the number is higher or lower than its guess We will go with educated guess lol
12th Apr 2017, 7:52 PM
LordHill
LordHill - avatar
+ 1
Nice code! In addition to what the others said, I'll add that you don't need to write "==True" in the loop condition, since it will evaluate to the value of run anyways. Also you could use elifs structures for the second and third ifs, since you only want to check them if the previous ifs conditions are not met (if you do this you won't​ need the continues anymore). Finally, in my opinion loops should end only when their condition is not met, so the code is easier to read. So I'd change the break for a run=False.
12th Apr 2017, 8:43 PM
Luca Garrera
0
I will try that. thanks for the tip!
12th Apr 2017, 8:13 PM
LordHill
LordHill - avatar
0
try my game gambet on google play where you play against gambet to guess its number and vice versa https://play.google.com/store/apps/details?id=mais.gambet&rdid=mais.gambet , it use a mathematical algorithm to guess your number by the shortest way.
12th Apr 2017, 8:20 PM
mohammad ammar
mohammad ammar - avatar
0
Downloading now. Happy to help support the community *Not bad, not bad at all
12th Apr 2017, 8:24 PM
LordHill
LordHill - avatar
0
Thanks for the tips Luca.. I know I don't need ==True, but I got in the habit and have just stuck with it. oddly, in Java I don't do that, only python lol. also, I normally change the loop condition when I want to stop, I am not sure why I chose break this time.. along with that, I am not sure when I started using continue either. My programming just kind of evolved to this for better or worse.
12th Apr 2017, 9:40 PM
LordHill
LordHill - avatar