Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Guessing game with difficult selection - my #3 code in python

just another code I made practicing, I will now try to read some documentation about the urllib library and try to make some different scripts, until now I only was able to make simple scripts like this one.


PHP Code

<?php 
import random
import time
import sys

for i in "GUESSING GAME CODED BY MALENTOR #3 SCRIPT EVER MADE":
     
time.sleep(0.08 )
     
sys.stdout.write(i)
     
sys.stdout.flush()


level_choice = int(input("""
1 = EASY MODE -> (0 - 10)
2 = MEDIUM MODE -> (0 - 50)
3 = EXPERT MODE -> (0 - 100)
INSERT YOUR MODE: """
))

easy = random.randint(0,10)
medium = random.randint(0,50)
expert = random.randint(0,100)

if 
level_choice == 1:

    while 
True:
        
# CHEATS ON -> print(easy)
        
userGuess = int(input("Your guess (0-10): "))
        if 
userGuess < easy:
            print(
"The number you entered is LOWER than the random one.")
        
elif userGuess > easy:
            print(
"The number you entered is HIGHER than the random one")
        else:
            print(
"CONGRATULATIONS, NUMBER HAS BEEN GUESSED")
            break

elif level_choice == 2:
        while 
True:
            
# CHEATS ON -> print(medium)
            
userGuess = int(input("Your guess (0-50): "))
            if 
userGuess < medium:
                print(
"The number you entered is LOWER than the random one.")
            
elif userGuess > medium:
                print(
"The number you entered is HIGHER than the random one")
            else:
                print(
"CONGRATULATIONS, NUMBER HAS BEEN GUESSED")
                break

elif level_choice == 3:
        while 
True:
            
# CHEATS ON -> print(expert)
            
userGuess = int(input("Your guess (0-100): "))
            if 
userGuess < expert:
                print(
"The number you entered is LOWER than the random one.")
            
elif userGuess > expert:
                print(
"The number you entered is HIGHER than the random one")
            else:
                print(
"CONGRATULATIONS, NUMBER HAS BEEN GUESSED")
                break 
Simple and good.Keep working mate.
why are you using sys module: (sys.stdout.write("")) in order to print something?
(Jul 8, 2017 05:33 PM)arkoydas Wrote:  Simple and good.Keep working mate.
why are you using sys module: (sys.stdout.write("")) in order to print something?

I believe just to put something into practice. Not necessary but good to learn
[Image: 5BgQiIm.gif]

(Jul 8, 2017 05:33 PM)arkoydas Wrote:  Simple and good.Keep working mate.
why are you using sys module: (sys.stdout.write("")) in order to print something?

hi,

i wanted to print the some text in loading style and I able to do it only using sys.stdout.write+ sys.stdout.flush
You should try by adding some try/catches just in case the user doesn't enter a integer number.
(Jul 9, 2017 06:04 AM)Hapi Wrote:  You should try by adding some try/catches just in case the user doesn't enter a integer number.

thank you , next codes will be using .try
Good work for your third ever script man.

You don't need to use if statements for the levels, dictionaries will come in handy for this. If you don't understand anything in the code below, search the documentation. PM me if you still don't get it or post in the Python section, everyone (mostly) is willing to help!

PHP Code

<?php 
import random
import time
import sys

for i in "GUESSING GAME CODED BY MALENTOR #3 SCRIPT EVER MADE":
    
time.sleep(0.08)
    
sys.stdout.write(i)
    
sys.stdout.flush()

# Dictionary to dictate which set of variables you're using when you select a difficulty.
diff = {
    
1: {
        
'name': 'Easy',
        
'max': 10,
    },

    
2: {
        
'name': 'Medium',
        
'max': 50,
    },

    
3: {
        
'name': 'Expert',
        
'max': 100,
    }
}

level = None # Initialise the variable as variables set in a try / except statement only work in that statement.
while True:
    try:
        
level = int(input("""
        1 = EASY MODE -> (0 - 10)
        2 = MEDIUM MODE -> (0 - 50)
        3 = EXPERT MODE -> (0 - 100)
        INSERT YOUR MODE: """
))

        
# Ensuring that the number is 1, 2 or 3
        
if(level < 1 or level > 3):
            
raise ValueError # Create a value error if it's not, so it runs the block of code below.

        
break

    
except ValueError:
        
# This block will run if they get a ValueError.
        # A VelueError raises when you you try to use the int() function on a string or float(?)
        
print('You must enter either: 1, 2 or 3')

number = random.randint(0, diff[level]['max'])

while 
True:
    
userGuess = int(input("Your guess (0-{}): ".format(diff[level]['max'])))
    if 
userGuess < number:
        print(
"The number you entered is LOWER than the random one.")
    
elif userGuess > number:
        print(
"The number you entered is HIGHER than the random one")
    else:
        print(
"CONGRATULATIONS, NUMBER HAS BEEN GUESSED")
        break 
Skype messages are NOT valid evidence, and here's why.


Possibly Related Threads...
Thread / Author Replies Views Last Post
by HackersFeb 5, 2025 09:07 AM 0 77 Feb 5, 2025 09:07 AM
Last Post: Hackers
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[C] by coginzitionSep 15, 2022 03:03 PM 6 351 Sep 15, 2022 03:03 PM
Last Post: Sajuuk