无法在循环中获得相等的变量

时间:2018-04-27 13:28:42

标签: python loops random input var

所以我只是想学习编程/编码。我试图制作一个循环,计算机随机猜出我输入的数字(变量),我主要是循环与“while”和“if / else”循环但但是... idk how将变量放入。我确定代码还有其他问题。它只是一个简单的,因为我实际上刚刚开始2天前。这是代码

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)

2 个答案:

答案 0 :(得分:3)

False

总是randintvar是随机函数r = randint(1,3) if r == var: ... 是一个整数(好吧,应该)。

你的意思是:

var = int(input())

(存储随机函数的结果以便能够显示它测试它,再次调用它会产生另一个值,显然)

是的,第一行应该是model.VAE.fit(...)才能输入整数值。

答案 1 :(得分:-1)

根据评论更新:

我刚刚制作了你的程序的工作版本,你的输入将是1,计算机将从1,2,3随机猜测,直到它给出正确的答案。

#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)

输出:

3
1
1
1
Good Bye!

Process finished with exit code 0