简体   繁体   English

python错误:无法分配给函数调用

[英]python error: can't assign to function call

This may be a stupid question, Why do i get this error? 这可能是一个愚蠢的问题,为什么会出现此错误? when I chane int(r) into r, it makes TypeError"unsupported operand type(s) for - 'str' and 'int'" I want to make the program that compare the YOURNUMBER with random number and repeat "n" times. 当我将int(r)转换为r时,它使TypeError“-'str'和'int'不受支持的操作数类型””我想使程序将YOURNUMBER与随机数进行比较,并重复“ n”次。 How can I solve this problem? 我怎么解决这个问题?

import random
r = raw_input("put YOURNUMBER")
while True: 
    n = raw_input("put repetition number")
    int(r)= int(r) - 1
    for x in range(10):
        x = random.randit()
    if int(r)!= 0:
        if int(n) > int(x):
            print "n > x"
        if float(n) == float(x):
            print "n = x"
        if float(n) < float(x):
            print "n < x"
    else:
        break
print "The end"

Convert r to an integer before entering the while loop. 进入while循环之前,将r转换为整数。 Similarly convert n to an integer when it is input. 类似地,将n输入时转换为整数。 Also it should be random.randint() , not random.randit() - and it requires some arguments for the range of values: 另外,它应该是random.randint() ,而不是random.randit() -并且它需要一些用于值范围的参数:

import random
r = int(raw_input("put YOURNUMBER"))
while True: 
    n = int(raw_input("put repetition number"))
    r = r - 1
    for x in range(10):
        x = random.randint(1, 10)
    if r != 0:
        if int(n) > int(x):
            print "n > x"
        if float(n) == float(x):
            print "n = x"
        if float(n) < float(x):
            print "n < x"
    else:
        break
print "The end"

If I understand you correctly: 如果我正确理解您的意见:

import random

r = raw_input("put YOURNUMBER: ")
r = int(r)


while True:
    n = raw_input("put repetition number: ")
    n = int(n)

    if n == 0:
        break

    for _ in xrange(n):
        x = random.randint(1, 10)

        if r > x:
            print "%s > %s" % (r, x)
        if r == x:
            print "%s = %s" % (r, x)
        if r < x:
            print "%s < %s" % (r, x)

print "The end"

Every variable has a type. 每个变量都有一个类型。 Function raw_input returns a string, that's why r and n are strings firstly. 函数raw_input返回一个字符串,这就是为什么rn首先是字符串。 Function int() converts variable into integer. 函数int()将变量转换为整数。 After your converting into integer you don't need to use int() for r and n again 转换为整数后,无需再次对rn使用int()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM