我有这个功能:
def compute():
if txtot.get() == "":
ot = 0
elif txtmed.get() == "":
med = 0
else:
bon = 0
total = ot+med+bon
print ("",total)
当被叫时加注:
UnboundLocalError: local variable 'med' referenced before assignment
知道如何解决?
答案 0 :(得分:0)
由于只会执行fn thing_that_uses_say_hello<G>(greeter: &G, name: &str) -> String
where
G: Greeting,
{
greeter.say_hello(name.into())
}
trait Greeting {
fn say_hello(&self, name: &str) -> String;
}
struct RealGreeting;
impl Greeting for RealGreeting {
fn say_hello(&self, name: &str) -> String {
format!("Hello, {}", name)
}
}
#[cfg(test)]
mod test {
use super::*;
use std::cell::RefCell;
struct MockGreeting<'a> {
called_with: RefCell<Vec<String>>,
value: &'a str,
}
impl<'a> MockGreeting<'a> {
fn new(value: &'a str) -> Self {
Self {
value,
called_with: Default::default(),
}
}
}
impl<'a> Greeting for MockGreeting<'a> {
fn say_hello(&self, name: &str) -> String {
self.called_with.borrow_mut().push(name.to_owned());
self.value.to_owned()
}
}
#[test]
fn test1() {
let g = MockGreeting::new("Hello");
let r = thing_that_uses_say_hello(&g, "Tom");
assert_eq!("Hello", r);
assert_eq!(&*g.called_with.borrow(), &["Tom".to_string()]);
}
}
语句的一个分支,因此在分支的所有三个中未分配值的变量将始终无法存在if语句。
在这种情况下,您的输出将始终为if-elif-else
,但通常您希望在if语句之前将变量初始化为某个值,并根据条件递增它们。
0