В следующем году я занимаюсь своей GCSE в области компьютерных наук, поэтому в настоящее время я работаю над прошлой контролируемой оценкой. Моя проблема, мои глобальные переменные не работают. У меня есть весь исходный код внизу. О, и я посмотрел на другой пост, но это не работает для меня.Python Глобальные переменные Прошлая бумага
#Controlled Assesment Task
import sys
import os
def register():
#Welcome Message
print("Welcome to the Exercise Tracker")
#Entering Details
print("Please complete the following fields to finish registration")
global firstname
firstname = input("FIRSTNAME: ")
global lastname
lastname = input("LASTNAME: ")
global age
age = int(input("AGE: "))
global height
height = float(input("HEIGHT: "))
global weight
weight = float(input("WEIGHT: "))
global email
email = input("EMAIL :")
#Excersie Intensity level
print("Choose an Excersie Intensity level")
excersise_level = input("High or Moderate: ")
#Generate ID
user_id1 = firstname[0:2]
user_id2 = lastname[0:3]
global user_id
user_id = user_id1 + user_id2
#User Information Check
#
#
#Display Information
infoCheck()
def infoCheck():
print("Please Check your Information")
print("User ID: " , user_id)
print("Firstname: " , firstname)
print("Lastname: " , lastname)
print("Age: " , age)
print("Height: " , height)
print("Weight" , weight)
print("Email: ", email)
print("Excercise Level: " , excersise_level)
print("==========================================================================")
print("Please Note: You can not change the User ID")
print("==========================================================================")
print("To edit Information enter the Field you like to Edit")
print("If you are done, write done")
#Ask if edit is required
edit_info = input("").lower()
#Info Edeting and after edit jumping back to edit
if edit_info == "firstname":
firstname = input("FIRSTNAME: ")
infoCheck()
elif edit_info == "lastname":
lastname = input("LASTNAME: ")
infoCheck()
elif edit_info == "age":
age = input("AGE: ")
infoCheck()
elif edit_info == "height":
height = float(input("HEIGHT: "))
infoCheck()
elif edit_info == "weight":
weight = float(input("WEIGHT"))
infoCheck()
elif edit_info == "email":
mail = input("EMAIL: ")
infoCheck()
elif edit_info == "level":
excersise_level = input("Enter Excersie Level (High or Moderate): ")
infoCheck()
#Back to start
register()
Надеется, что кто-то может помочь мне :) Большому спасибо заранее :)
Прежде всего - использование глобальных варсов - это плохая практика сама по себе. Второе - вы злоупотребляете ими, как уже было сказано. – konart
Действительно ли программа GCSE рекомендует использовать глобальные переменные? Они почти всегда плохая идея, и нет смысла использовать их в этом коде. –