UnboundLocalError:分配前已引用局部变量“ tickets_per_person”,但已将全局变量“ tickets_per_person”分配为全局变量

时间:2019-05-01 11:03:37

标签: python user-interface tkinter thonny

我不断收到此错误:

  

UnboundLocalError:引用了本地变量“ tickets_per_person”   在分配之前。

但是我在代码的顶部将变量sudo Certificates.command 分配为全局变量,所以我不明白为什么它不能被识别。

完整功能是:

tickets_per_person

分配全局变量:

def date_price_increase() :
    if 'July' or 'August' or 'September' in full_date :
        tickets_per_person = tickets_per_person * 1.15
    elif 'February' or 'October' or 'November'in full_date :
        tickets_per_person = tickets_per_person * 0.95
    else :
        tickets_per_person = tickets_per_person    

回溯是:

global tickets_per_person
tickets_per_person = 0

1 个答案:

答案 0 :(得分:2)

您必须在for内使用<?php session_start(); require 'lib/password.php'; require 'connect.php'; if(isset($_POST['login'])){ $username = !empty($_POST['username']) ? trim($_POST['username']) : null; $passwordAttempt = !empty($_POST['password']) ? trim($_POST['password']) : null; $sql = "SELECT id, username, password FROM users WHERE username = :username"; $stmt = $pdo->prepare($sql); $stmt->bindValue(':username', $username); $stmt->execute(); $user = $stmt->fetch(PDO::FETCH_ASSOC); if($user === false){ die('Incorrect username / password combination!'); } else{ $validPassword = password_verify($passwordAttempt, $user['password']); if($validPassword){ $_SESSION['username'] = $user['username']; $_SESSION['logged_in'] = time(); header('Location: pages/manageinformation.php'); exit; } else{ die('Incorrect username / password combination!'); } } } ?> 来通知函数您要使用全局变量global tickets_per_person而不是局部变量。

def date_price_increase():

由于在函数外部创建的所有变量都是全局变量,因此无需在函数外部使用单词tickets_per_person


编辑:您应该使用参数和def date_price_increase() : global tickets_per_person if 'July' or 'August' or 'September' in full_date : tickets_per_person = tickets_per_person * 1.15 elif 'February' or 'October' or 'November'in full_date : tickets_per_person = tickets_per_person * 0.95 else : tickets_per_person = tickets_per_person 创建普通函数。这是首选方法。

global

并运行

return