Skip to main content
Tweeted twitter.com/StackCodeReview/status/1499988078081056769
Became Hot Network Question
edited tags
Link
Reinderien
  • 71.1k
  • 5
  • 76
  • 256
Spelling and grammar
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

Improving a Python random number guessing game code from a beginner programmer

Hello dear stackexchange Community :) I'veI've been learning Python for about 4 days now. Python is my first programming language, accordingly imso I'm not that good at coding yet. I have been programming a little random number game as my first project and came to a state of code with which imI'm kind of happy. 

My code does what iI want,: it generates a random number between 1 and 20, prints the initial tip and asks the user offor an input guess. Should iI guess right it completes the round, adds 100 points to my score and continues with round 2. When i didI do not guess correctcorrectly, it substractssubtracts 20 points off my score and asks me to try again for another 2 attempts until iI lose. Now my question would be, in

In which way iways can I improve my code.? What i mean by this are the weaknesses of this code and what could be ways to write thatmake code more efficient/generally better/more clear whatsoever. So in general im asking for little tips to shape(or reshape if its a complete failure)my code. I hope this question isnt too shallow im still very new in this forum and i excuse myself for eventual grammar mistakes, english is not my first language. Hope you have a nice day :) Greetings, Marcclearer and generally better?

Improving a Python random number game code from a beginner programmer

Hello dear stackexchange Community :) I've been learning Python for about 4 days now. Python is my first programming language, accordingly im not that good at coding yet. I have been programming a little random number game as my first project and came to a state of code with which im kind of happy. My code does what i want, it generates a random number between 1 and 20, prints the initial tip and asks the user of an input guess. Should i guess right it completes the round, adds 100 points to my score and continues with round 2. When i did not guess correct, it substracts 20 points off my score and asks me to try again for another 2 attempts until i lose. Now my question would be, in which way i can improve my code. What i mean by this are the weaknesses of this code and what could be ways to write that code more efficient/generally better/more clear whatsoever. So in general im asking for little tips to shape(or reshape if its a complete failure)my code. I hope this question isnt too shallow im still very new in this forum and i excuse myself for eventual grammar mistakes, english is not my first language. Hope you have a nice day :) Greetings, Marc

Python random number guessing game

I've been learning Python for about 4 days now. Python is my first programming language, so I'm not that good at coding yet. I have been programming a little random number game as my first project and came to a state of code with which I'm kind of happy. 

My code does what I want: it generates a random number between 1 and 20, prints the initial tip and asks the user for an input guess. Should I guess right it completes the round, adds 100 points to my score and continues with round 2. When I do not guess correctly, it subtracts 20 points off my score and asks me to try again for another 2 attempts until I lose.

In which ways can I improve my code? What are the weaknesses of this code and what could be ways to make code more efficient, clearer and generally better?

Source Link

Improving a Python random number game code from a beginner programmer

Hello dear stackexchange Community :) I've been learning Python for about 4 days now. Python is my first programming language, accordingly im not that good at coding yet. I have been programming a little random number game as my first project and came to a state of code with which im kind of happy. My code does what i want, it generates a random number between 1 and 20, prints the initial tip and asks the user of an input guess. Should i guess right it completes the round, adds 100 points to my score and continues with round 2. When i did not guess correct, it substracts 20 points off my score and asks me to try again for another 2 attempts until i lose. Now my question would be, in which way i can improve my code. What i mean by this are the weaknesses of this code and what could be ways to write that code more efficient/generally better/more clear whatsoever. So in general im asking for little tips to shape(or reshape if its a complete failure)my code. I hope this question isnt too shallow im still very new in this forum and i excuse myself for eventual grammar mistakes, english is not my first language. Hope you have a nice day :) Greetings, Marc

import random


attempt = 1
user_input = "ab"


class Test:


    def __init__(self):

        self.score = 100
        self.testguess = 0
        self.y = len("yes")
        self.z = len("no")
        self.number_holder = 0

    def number_gen(self):
        
        self.number_holder = random.randrange(1,20)     

        
    def first_tip(self):
        
        if(self.number_holder % 2 == 0 and self.number_holder <= 10):
            print("*Your number is even and smaller than 10 or the same")
        elif(self.number_holder % 2 == 0 and self.number_holder >= 10):
            print("*Your number is even and bigger than 10 or the same")
        elif(self.number_holder % 2 != 0 and self.number_holder <= 10):
            print("*Your number is uneven and smaller than 10 or the same")
        elif(self.number_holder % 2 != 0 and self.number_holder >= 10):
            print("*Your number is uneven and bigger than 10 or the same")
        
        print("*************************************")
        print(f"*{self.number_holder}")


    def make_try(self):
        
        print("*************************************")
        print("*My guess would be:")
        print("*************************************")
        self.testguess = int(input("*")) 
                    
            
    def check_win(self, attempt):

        if(self.number_holder == self.testguess):
                       
            self.score = self.score
            print("*************************************")
            print(f"*You have won, your final score was: {self.score}")
            print("*************************************")
            return True
            
        
        elif(self.number_holder != self.testguess and self.testguess != 0): 
            
            attempt_calc = 3 - attempt
            self.score = self.score - 20
            print("*************************************")
            print(f"*That was not correct. Your new score: {self.score}")
            print("*************************************")
            print(f"*You have {attempt_calc} attempts left.")
            return False
            

    def start_again(self, user_input, attempt):
        
        x = len(user_input)

        if(x == self.y):
            return True

     
        elif(x == self.z):

            return False
        
        else:
            print("*************************************")
            print("*Wrong input")
            print("*Do you want to play the NumberGame?")
            print("*Type Yes/yes or No/no")
            print("*************************************")
            user_input = input("*")



if __name__ == "__main__":


    T1 = Test()   
 
    for i in range(3):

        print(i)
        
        T1.number_gen()

        while attempt <= 3 and user_input != "no" and user_input != "No":

            
    
            if(attempt == 1):
                print("*************************************")
                print("*Do you want to play the NumberGame?[Yes/No]")
                print("*************************************")
                user_input = input("*")
                ("*************************************")
                
            
            elif(attempt == 2):
                print("*************************************")
                print("*Do you want to try again?[Yes/No]")
                print("*************************************")
                user_input = input("*")
                print("*************************************")
            
            elif(attempt == 3):
                print("*************************************")
                print("*Do you want to try again?[Yes/No]")
                print("*************************************")
                user_input = input("*")
                print("*************************************")
                
                
            if(T1.start_again(user_input, attempt) == False):
                print("*************************************")
                print("*See you next Time!")
                print("*************************************")
                break
    
            if(attempt == 1):
                T1.first_tip()
    
            T1.make_try()
    
            if(T1.check_win(attempt) == True):
                break
                   
            elif(attempt == 3):
                print("*************************************")
                print("*No more attempts.")
                print("*************************************")
                break
            
            attempt = attempt + 1

            print("*************************************")
            print("*Thanks for playing")
            print("*************************************")
        
        attempt = 1