eval():无法分配给函数调用 [英] eval(): can't assign to function call

查看:100
本文介绍了eval():无法分配给函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Cookie答题器还有另一个问题...这是我制作的会产生错误的代码:

Yet another question for my cookie clicker... Here is the code I made that produces an error:

cps=cps+bcps[buych]
c=c-bprice[buych]
eval(buych)=eval(buych)+1

cps是一个变量,
c是变量,
b1,b2,b3,b4,b5是变量
buych是一个字符串(我从input()命令中获得)
bcps和bprice是地图(如下所示)

cps is a variable,
c is a variable,
b1,b2,b3,b4,b5 are variables
buych is a string (Which I get from an input() command)
bcps and bprice are maps (shown below)

bcps = {"b1":'1',
    "b2":'5',
    "b3":'10',
    "b4":'20',
    "b5":'25'}
bprice = {"b1":'10',
    "b2":'20',
    "b3":'30',
    "b4":'40',
    "b5":'50'}

所以,我想要实现的是:
-取输入字符串值,将是'b1','b2','b3','b4','b5'
-增加cps购买bcps内的值
-通过bprice中的值减少c
-使用eval将'b#'转换为b#
-将b#增加1

So, what I'm trying to achieve is:
-Take the input string value, which will be 'b1','b2', 'b3', 'b4', 'b5'
-Increase cps buy it's value within bcps
-Decrease c by it's value within bprice
-Use eval to convert the 'b#' to b#
-Increase b# by 1

运行脚本时,我在python shell中没有得到错误文本.取而代之的是,我看到一个弹出窗口,提示无法分配给函数调用".该错误突出显示为eval()之前的第一个空格.我真的不明白这一点,因为我在错误中没有任何功能.

When running the script, I do not get error text in the python shell. Instead, I get a pop-up that says "Can't assign to function call". The error is highlighted as the first white space before the eval(). I really don't get this, as I have no functions within the error.

感谢阅读,
Cookie怪物

Thanks for reading,
Cookie Monster

推荐答案

eval(buych)是一个函数调用-您正在调用函数 eval().您只能分配给变量,而不能分配给函数的结果.

eval(buych) is a function call -- you're calling the function eval(). You can only assign to variables, you can't assign to the result of a function.

您需要将变量连接到一个有效的赋值表达式中,并执行 exec :

You need to concatenate the variable into a valid assignment expression, and exec that:

exec(buych + '=' + buych + '+1')

eval 用于评估表达式,您必须使用 exec 执行整个语句.

eval is for evaluating expressions, you have to use exec to execute a whole statement.

但是,如果您发现自己需要这样做,通常就是在做错事.您应该使用列表或字典,而不是计算变量名称和表达式,以便可以使用索引或键来引用它们.

But if you find yourself needing to do this, you're usually doing something wrong. Instead of computing variable names and expressions, you should use lists or dicts so you can refer to them using an index or key.

这篇关于eval():无法分配给函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆