如何使用Python在用户屏幕上做出任何声明几秒钟后消失?

问题描述 投票:0回答:2

我是一个学习Python的初学者,我做了一个简单的“石头、剪刀、布”游戏。

我的程序要求用户输入以下选项之一(“石头,布,剪刀”)。它将显示谁是获胜者。但玩了几次后,我必须开始向下滚动很远才能再次玩。这很烦人。

我的代码:

import random
import time 
print("Welcome to Rock, Paper, Scissors !")
name = input("Enter a Username:", )
print("Hi", name)
keep_going = 'y'
while keep_going == 'y' or 'Y':
    Users_choice = input("Choose: \nRock:\nPaper:\nScissors:")
    print("You choose:", Users_choice)
    computers_choice = random.randint(1,3)
    if computers_choice == 1:
        computers_choice = 'Rock'
        print("The Computer choice is:", computers_choice)
    
    elif computers_choice == 2:
        computers_choice = 'Scissors'
        print("The Computer choice is:", computers_choice)
    else:
        computers_choice == 3
        computers_choice = 'Paper'
        print("The Computer choice is:", computers_choice)
    
    if Users_choice == 'Rock' and computers_choice == 'Scissors':
        print("The winner is . . . .")
        print(name, "!")
    elif computers_choice == 'Rock' and Users_choice == 'Scissors':
        print("The winner is . . . .")
        print("YOU GOT OUTSMARTED BY A COMPUTER!!")
    elif Users_choice == 'Scissors' and computers_choice == 'Paper':
        print("The winner is . . . .")
        print(name, "!")
    elif computers_choice == 'Scissors' and Users_choice == 'Paper':
        print("The winner is . . . .")
        print("YOU GOT OUTSMARTED BY A COMPUTER!!")
    elif Users_choice == 'Rock' and computers_choice == 'Paper':
        print("The winner is . . . .")
        print("YOU GOT OUTSMARTED BY A COMPUTER")
    elif computers_choice == 'Rock' and Users_choice == 'Paper':
        print("The winner is . . . .")
        print(name, "!")
    elif Users_choice == 'Rock' and computers_choice == 'Rock':
        print("It's a TIE!")
    elif Users_choice == 'Scissors' and computers_choice == 'Scissors':
        print("It's a TIE!")
    else:
        Users_choice == 'Paper' and computers_choice == 'Paper'
        print("It's a TIE!")
    keep_going = input("Press Y or y is you want to keep playing!:")



    

这是输出:

nline Python beta

AD
    


 
    
Welcome to Rock, Paper, Scissors !
Enter a Username:
WHY WHY
Hi WHY WHY
Choose: 
Rock:
Paper:
Scissors:
ROck
You choose: ROck
The Computer choice is: Scissors
It's a TIE!
Press Y or y is you want to keep playing!:
Y
Choose: 
Rock:
Paper:
Scissors:
ROck
You choose: ROck
The Computer choice is: Scissors
It's a TIE!
Press Y or y is you want to keep playing!:
Y
Choose: 
Rock:
Paper:
Scissors:
ROck
You choose: ROck
The Computer choice is: Scissors
It's a TIE!
Press Y or y is you want to keep playing!:

它不断地一遍又一遍地打印同样的东西。

你可以使用时间让它在宣布获胜者几秒钟后消失吗?

我用谷歌搜索了一下,有点困惑,但可以:

time.sleep(10)

解决问题,如果是的话如何使用它?

python time
2个回答
0
投票

用这个

import time
import os

time.sleep(10) # sleep for 10 seconds
os.system('cls') # for linux use: os.system('clear')

所以基本上你会睡 10 秒,然后清除控制台


0
投票

您可以使用以下方式清除屏幕(作为

while
循环的第一行):

  1. print('\033[2J\033[H')
    如果您的终端支持 ANSI 转义序列。
  2. os.system('cls' if os.name == 'nt' else 'clear')
© www.soinside.com 2019 - 2024. All rights reserved.