-2

How do I reset the random integer when I guessed right. I'm a beginner and could you give me some tips, that help me to write an better code.

from random import randint
##Guess a number from 1 to 100
##Made by 
##vers. 0.01beta

name = str(input("how ya name bruh:"))

randm = randint(1, 99)

guesstaken = str(int(0))

while True:
    user_guess = int(input(name + " " + "enter a number:"))

    if user_guess == randm:
       print("you guessed" + " " + "right" + ", " + name)
       SystemExit

    elif user_guess >= randm:
       print("guess lower")
       guesstaken = guesstaken + str(int(1))

    elif user_guess <= randm:
       print("guess higher")
    guesstaken = guesstaken + str(int(1))
nbro
  • 15,395
  • 32
  • 113
  • 196
  • 1
    Do `randm = randint(1, 99)` again – Patrick Haugh Mar 03 '17 at 20:12
  • Out of curiosity, why are you doing `name = str(input("how ya name bruh:"))` instead of just `name = input("how ya name bruh:")`? I've been seeing this style a lot among question-askers lately and I want to know where the trend's coming from. – Kevin Mar 03 '17 at 20:19
  • i had erors and by adding this they dont showed up after – master_of_not_knowlege Mar 03 '17 at 20:20
  • @Kevin that's a (bad) way to make it portable for python 2 and 3 (instead of raw_input). And that doesn't hurt much: http://stackoverflow.com/questions/42223125/should-i-avoid-converting-to-a-string-if-a-value-is-already-a-string apart from the security issue of `input` in python 2. – Jean-François Fabre Mar 03 '17 at 20:20
  • [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – EJoshuaS - Stand with Ukraine Mar 03 '17 at 20:22
  • 1
    @Jean-FrançoisFabre But if I enter "Steve" for that prompt, it's going to crash with a NameError in Python 2 regardless of whether you put a str() around it or not. – Kevin Mar 03 '17 at 20:22
  • @master_of_not_knowlege, that's interesting... What error in particular did you get? – Kevin Mar 03 '17 at 20:23
  • @Kevin. not if you put it between quotes; no but I see your point. It's useless, yes. – Jean-François Fabre Mar 03 '17 at 20:24
  • @Kevin if i remove str it shows this error TypeError: '>=' not supported between instances of 'str' and 'int' – master_of_not_knowlege Mar 03 '17 at 20:29
  • Interesting. That happens if you remove the `str` from line 6, `name = str(input("how ya name bruh:"))`? Are you sure you aren't removing something from a different `input` call? Because I'd only expect that error to occur if you removed the `int()` from line 13, `user_guess = int(input(name + " " + "enter a number:"))` – Kevin Mar 03 '17 at 20:32
  • To cut directly to my point: you seem to have adopted a policy of "always explicitly convert the return value of `input` to the type you want the value to be", but in reality this is only necessary if you want the type to be something other than a string. `input` always returns a string, so no conversion is necessary if you want it to be a string. (In Python 3.X) – Kevin Mar 03 '17 at 20:36
  • @Kevin i removed it from line 6 and an error appeared. Now i wrote it back in and deleted it from line 13 and the same error appears again – master_of_not_knowlege Mar 03 '17 at 20:40

2 Answers2

0

There are a couple of problems with the code you posted. As mentioned in the comments, the str() call in name = str(input("how ya name bruh:")) is pointless because the Python 3 input() function always returns a string; calling str() on an object that's already a string just returns that object. So it doesn't hurt anything, but it adds unnecessary clutter to the code.

Similarly, the expression int(1) is redundant because 1 is already an integer. And calling int() on an integer object just returns the original integer object.

However, guesstaken = guesstaken + str(int(1)) is strange. It doesn't perform arithmetic it does string concatenation because both guesstaken and str(int(1)) are strings. Here's a short demo of what it does.

s = '0'
for i in range(5):
    s = s + '1'
    print(s)

output

01
011
0111
01111
011111

With that stuff out of the way we can get onto your question. :) You want to know how to get a new random integer once the player has guessed the correct number. That's simple: we just put the code inside another while loop. We also need to ask the player if they want to play again so that we have some way of breaking out of that loop and exiting the program.

Here is a repaired version of your code, with that extra while loop. I've used the string .format() method to make the printing code cleaner.

''' Guess a number from 1 to 100 '''

from random import randint

name = input("What's your name? ")
prompt = name + " enter a number: "

while True:
    randm = randint(1, 99)
    guesstaken = 0
    while True:
        user_guess = int(input(prompt))
        guesstaken += 1

        if user_guess == randm:
            print("You guessed {} {}, in {} guesses".format(randm, name, guesstaken))
            # break out of inner loop
            break
        elif user_guess >= randm:
            print("Guess lower")
        elif user_guess <= randm:
            print("Guess higher")

    # Play again if user inputs "y" or "Y"
    if input("Play again? [y/N] ").lower() != "y":
        # break out of outer loop
        break

print("Thanks for playing, {}.".format(name))

demo

What's your name? PM 2Ring
PM 2Ring enter a number: 50
Guess lower
PM 2Ring enter a number: 25
Guess higher
PM 2Ring enter a number: 37
Guess lower
PM 2Ring enter a number: 31
Guess higher
PM 2Ring enter a number: 34
Guess lower
PM 2Ring enter a number: 33
Guess lower
PM 2Ring enter a number: 32
You guessed 32 PM 2Ring, in 7 guesses
Play again? [y/N] y
PM 2Ring enter a number: 64
Guess lower
PM 2Ring enter a number: 32
Guess lower
PM 2Ring enter a number: 16
Guess higher
PM 2Ring enter a number: 24
Guess higher
PM 2Ring enter a number: 28
Guess lower
PM 2Ring enter a number: 26
You guessed 26 PM 2Ring, in 6 guesses
Play again? [y/N] n
Thanks for playing, PM 2Ring.
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
-1

Just assign randm to randint(1, 99) (do randm = randint(1,99) again).

Also, if what you want is Python to forget the variable use del, but it's completely unnecessary.

Juan T
  • 1,219
  • 1
  • 10
  • 21