Lỗi nó trả về là:
NameError: name 'lives' is not definedTôi biết mã không hiệu quả nhất có thể, đây là một trong những dự án đầu tiên của tôi, tuy nhiên bất cứ điều gì tôi cố gắng làm lỗi này đều xuất hiện, tôi đã thử tạo ra một toàn cầu cho nó nhưng điều đó không giúp ích gì. Tôi thực sự sẽ đánh giá cao một số trợ giúp với điều này, cảm ơn!
import random import time def main(): global guess,rand_num win = False rand_num = 45 lives = 10 while lives > 0 and win == False: guess = int(input("Guess a number!")) compare() print("Well done!") time.sleep(3) def compare(): global lives,win if guess == rand_num: print("You guessed correct!") win = True elif guess > rand_num: print ("Guess lower!") lives = lives - 1 else: print ("Guess higher!") lives = lives - 1 def repeat(): replay = input("would you like to play again? Y/N") if replay == "Y": print("enjoy!") main() elif replay == "N": "Goodbye then, hope you enjoyed!" time.sleep(3) os._exit else: print("please enter Y or N") repeat() main() repeat()Chỉnh sửa: Đặt cuộc sống toàn cầu bên trong Main () trả về lỗi:
UnboundLocalError: local variable 'lives' referenced before assignmentĐã hỏi ngày 21 tháng 6 năm 2016 lúc 21:43Jun 21, 2016 at 21:43
KaoskaosKaos
691 Huy hiệu vàng1 Huy hiệu bạc7 Huy hiệu đồng1 gold badge1 silver badge7 bronze badges
10
Bạn cần xác định biến "cuộc sống" bên ngoài hàm chính, sau đó bất kỳ chức năng nào bạn muốn tham khảo biến toàn cầu mà bạn nói là "cuộc sống toàn cầu". Khi bạn ở trong một hàm và gán một giá trị cho một biến, nó giả định nó nằm trong phạm vi cục bộ. Sử dụng "cuộc sống toàn cầu" cho biết chức năng đó để xem xét phạm vi toàn cầu như là tài liệu tham khảo của cuộc sống.
import random import time lives = 10 win = False guess = 0 rand_num = 45 def main(): global guess, rand_num, lives, win win = False rand_num = 45 lives = 10 while lives > 0 and win == False: guess = int(input("Guess a number!")) compare() print("Well done!") time.sleep(3) def compare(): global guess, rand_num, lives, win if guess == rand_num: print("You guessed correct!") win = True elif guess > rand_num: print ("Guess lower!") lives = lives - 1 else: print ("Guess higher!") lives = lives - 1 def repeat(): replay = input("would you like to play again? Y/N") if replay == "Y": print("enjoy!") main() elif replay == "N": "Goodbye then, hope you enjoyed!" time.sleep(3) os._exit else: print("please enter Y or N") repeat() main() repeat()Đã trả lời ngày 21 tháng 6 năm 2016 lúc 22:02Jun 21, 2016 at 22:02
Bạn đã không tuyên bố lives là toàn cầu bên trong main(), vì vậy nó là địa phương cho chức năng đó.
def main(): global guess, rand_num, lives ...Đã trả lời ngày 21 tháng 6 năm 2016 lúc 21:44Jun 21, 2016 at 21:44
Chepnerchepnerchepner
465K68 Huy hiệu vàng491 Huy hiệu bạc636 Huy hiệu Đồng68 gold badges491 silver badges636 bronze badges
Khi bạn khai báo nó bên trong hàm, chúng chỉ có sẵn trong phạm vi chức năng đó, vì vậy hãy khai báo các biến toàn cầu bên ngoài các hàm và mã sẽ hoạt động tốt.
import random import time guess = None random_num = None lives = 3 win = False def main(): global guess,rand_num win = False rand_num = 45 lives = 10 while lives > 0 and win == False: guess = int(input("Guess a number!")) compare() print("Well done!") time.sleep(3) def compare(): global lives,win if guess == rand_num: print("You guessed correct!") win = True elif guess > rand_num: print ("Guess lower!") lives = lives - 1 else: print ("Guess higher!") lives = lives - 1 def repeat(): replay = input("would you like to play again? Y/N") if replay == "Y": print("enjoy!") main() elif replay == "N": "Goodbye then, hope you enjoyed!" time.sleep(3) os._exit else: print("please enter Y or N") repeat() main() repeat()Và bây giờ điều này hoạt động tốt. Để biết thêm thông tin về các biến gloval vs cục bộ bạn có thể đọc: //www.python-course.eu/global_vs_local_variables.php
Đã trả lời ngày 21 tháng 6 năm 2016 lúc 21:48Jun 21, 2016 at 21:48
TeemoteemoTeemo
4396 Huy hiệu bạc21 Huy hiệu Đồng6 silver badges21 bronze badges
2