简体   繁体   English

我的python脚本突然停止

[英]My python script suddenly stops

My python script suddenly stops when it executes a certain piece of code and before that it was working fine but now it like crashes when it executes it Here is my code: 我的python脚本在执行某段代码时突然停止运行,并且在此之前运行良好,但是现在执行时就像崩溃一样。这是我的代码:

import random; from time import sleep
while True:
    if 'playerName' in locals() or 'playerName' in globals():
        continue
    else:
        print('What is your name?')
        playerName = input()
    print(f'Hi there, {playerName}, welcome to this math game.')
    sleep(2)
    print('This game can make yourself custom equations to test your math. (only addition.)')
    sleep(2)
    print('Set the limit for numbers. (example. 1000 and 1234)')
    limit1 = int(input())
    limit2 = int(input())
    number1 = random.randint(limit1, limit2)
    number2 = random.randint(limit1, limit2)
    answer = number1 + number2
    print(f'Here is the question: {number1} + {number2}')
    sleep(1.5)
    print('Do you know the answer for this?')
    playerAnswer = input()
    if int(str(playerAnswer)) == answer:
        print(f'Good job, {playerName} You answered correctly!')
    elif int(str(playerAnswer)) != answer:
        print('Nope the answer is ' + str(int(answer)) + '.')
    print('Do you want to restart? (y/n)')
    restart = input()
    if restart == 'y':
        continue
    else:
        print('Goodbye')
        break

It stops at the very end once it finishes one round. 一旦结束一圈,它就会在最后停止。 Can someone help me with this? 有人可以帮我弄这个吗?

if 'playerName' in locals() or 'playerName' in globals():
    continue

This statement will be only one executed after player will choose "Yes" and will not let you go to the next "print" statement, because 'playerName' will be always in locals() after first round of game. 在玩家选择“是”并且不允许您进入下一个“打印”语句之后,该语句将仅执行,因为“ playerName”在游戏的第一轮之后将始终位于locals()中。

Simple fix can look like that: 简单修复如下所示:

if not ('playerName' in locals() or 'playerName' in globals()):
    print('What is your name?')
    playerName = input()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM