Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法在循环中获得相等的变量_Python_Loops_Random_Input_Var - Fatal编程技术网

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

Python 无法在循环中获得相等的变量,python,loops,random,input,var,Python,Loops,Random,Input,Var,所以我只是想学习编程/编码。我试着做一个循环,计算机随机猜测我输入的一个数字(变量),我主要是用“while”和“if/else”循环,但是像…idk如何输入变量。我肯定代码还有其他问题。这只是一个简单的问题,因为我实际上是两天前才开始的。这是密码 input = var x = 0 counter = 0 while x == 0: from random import * print randint(1, 3) if randint == var:

所以我只是想学习编程/编码。我试着做一个循环,计算机随机猜测我输入的一个数字(变量),我主要是用“while”和“if/else”循环,但是像…idk如何输入变量。我肯定代码还有其他问题。这只是一个简单的问题,因为我实际上是两天前才开始的。这是密码

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)
总是
为False
randint
是随机函数,
var
是一个整数(应该是)

你的意思是:

r = randint(1,3)
if r == var:
   ...
(存储随机函数的结果以便能够显示和测试它,再次调用它显然会产生另一个值)

是的,第一行应该是
var=int(input())
才能输入整数值

总是
为False
randint
是随机函数,
var
是一个整数(应该是)

你的意思是:

r = randint(1,3)
if r == var:
   ...
(存储随机函数的结果以便能够显示和测试它,再次调用它显然会产生另一个值)


是的,第一行应该是
var=int(input())
才能输入整数值。

根据注释更新:

我刚刚制作了一个你的程序的工作版本,你的输入是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

根据评论更新:

我刚刚制作了一个你的程序的工作版本,你的输入是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

randint==var
不能为真,您正在将函数与整数进行比较。
input
是一个内置函数。不要将
var
分配给它。是的,您的代码存在一些关键问题。我建议暂时远离代码,花点时间学习Python的基础知识。一个好的起点是。
randint==var
不能为真,您正在将函数与整数进行比较。
input
是一个内置函数。不要将
var
分配给它。是的,您的代码存在一些关键问题。我建议暂时远离代码,花点时间学习Python的基础知识。一个好的起点是。
从随机导入*
开始不好,在循环中甚至更坏。
从随机导入*
开始不好,在循环中甚至更坏。