0

So I'm just trying to learn programming/coding. and I'm trying to make a loop where the computer guesses at random a number that I put in (the variable), I mostly the loop with the "while" and "if/else" loops down but like...idk how to put the variable in. I'm sure there are other things wrong with the code. Its just a simple one since I actually just started 2 days ago. here is the code

input = var
x = 0
counter = 0

while x == 0:
    from random import *
    print randint(1, 3)

    if randint == var:
        x = 1
        count = counter + 1
        print (counter)
        print "Good Bye!"
    else:
        x == 0
        counter = counter + 1
        print (counter)
  • `randint == var` cannot be true, you're comparing the _function_ with an integer. – Jean-François Fabre Apr 27 '18 at 13:32
  • 1
    `input` is a built-in function. Don't assign `var` to it. – Fernando Cezar Apr 27 '18 at 13:32
  • Yes, there are some key problems with your code. I suggest stepping away from your code for a bit and taking the time to learn the fundamentals of Python. A good place to start would be [_The Python Tutorial_](https://docs.python.org/3/tutorial/#the-python-tutorial). – Christian Dean Apr 27 '18 at 13:33

2 Answers2

3
if randint == var:

is always False. randint is the random function, var is an integer (well, it should be).

You mean:

r = randint(1,3)
if r == var:
   ...

(store the result of the random function to be able to display it and test it, calling it again yields another value, obviously)

and yes, the first line should be var = int(input()) to be able to input an integer value.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
-1

Updated according to the comments:

I just made a working version of your program, your input would be 1, and computer would randomly guess from 1,2,3 until it gives the correct answer.

#input = var this line is wrong as pointed out by others
input =  1 # this is your input number
x = 0
counter = 0
import random

while x == 0:
    #from random import * this will import too much in the while loop according to comments

    #randint(1, 3) this line is wrong
    randint = random.randint(1, 3) # computer guess from 1-3, then you should assign random generated number to randint
    print(randint)

    # if randint == var:  this line is wrong, you can't compare randint to var.
    if randint == input: #
        x = 1
        count = counter + 1 
        print(counter)
        print("Good Bye!")
    else:
        x = 0
        counter = counter + 1
        print(counter)

output:

3
1
1
1
Good Bye!

Process finished with exit code 0
Tianyun Ling
  • 1,045
  • 1
  • 9
  • 24