Hack Forums
[Python] Guessing game with difficult selection - my #3 code in python - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Guessing game with difficult selection - my #3 code in python (/showthread.php?tid=5664899)



Guessing game with difficult selection - my #3 code in python - Malentor - Jul 8, 2017

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 



* - arkoydas - Jul 8, 2017

Simple and good.Keep working mate.
why are you using sys module: (sys.stdout.write("")) in order to print something?


* - TheWarOwl - Jul 8, 2017

(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


* - Malentor - Jul 9, 2017

(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


* - Hapi - Jul 9, 2017

You should try by adding some try/catches just in case the user doesn't enter a integer number.


* - Malentor - Jul 9, 2017

(Jul 9, 2017 06:04 AM)Looka 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


* - Ensky - Jul 10, 2017

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