MentalChai Newsletter

Share this post
Python Code – Guessing Game
mentalchai.substack.com

Python Code – Guessing Game

Recovered Blog Post from Sept 5, 2015

Ali Ismail
Jan 28, 2021
Share this post
Python Code – Guessing Game
mentalchai.substack.com

The asset of creating.
I want to pass it on.
I want to pass it forward.
To create something from nothing.

Lately I’ve been trying to get my cousins to learn how to program. They’re in their late teens and deciding where they want to take their lives. Regardless of where they go and what field they end up studying, having the ability to create tools as you need them is an indispensable asset.

Games are really fun, and I think it’s a great place to start challenging oneself.

I’ve started with a number guessing game where the computer picks a number between 1 and 10 and the player has to guess what it is.

They haven’t learned functions yet, but they’re beginning to learn how to apply if-then logic and flow data in and out of loops.

You can find the code below.
Next on the agenda is “Rock, Paper, Scissors!”

Toodles,

#!/usr/bin/pythonimport random
guess = 0count = 0secret = random.randint(1,10)
while( guess != secret):print ‘Select a number between 1 and 10: ‘guess = raw_input()count += 1
if (int(guess) == secret):print ‘correct’break;else:
print ‘the correct answer is: ‘ + str(secret) + ‘\n you selected: ‘ + str(guess)#        print ‘You (incorrectly) guessed ‘ + str(guess) + ‘ please try again’

print ‘only took you ‘ + str(count) + ‘ guesses’
Share this post
Python Code – Guessing Game
mentalchai.substack.com
Comments

Create your profile

0 subscriptions will be displayed on your profile (edit)

Skip for now

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.

TopNew

No posts

Ready for more?

© 2022 Ali Ismail
Privacy ∙ Terms ∙ Collection notice
Publish on Substack Get the app
Substack is the home for great writing