-1
print ('hello, welcome to our bar')
age = int(input("What is your age"))

if age < 21:
    print ('*kicks your ass out of bar*')
else:
    print("come on in") 

我可以运行代码并让它询问一个数字,一旦你在数字下输入数字,就会出现所需的输出。想要的是而不是输出看起来像

hello, welcome to our bar
What is your age25
come on in

我希望它看起来像

come on in

非常新很抱歉,如果这非常简单,我一直在寻找以前问过这个问题的人,但我找到的代码都没有工作

4

2 回答 2

2

您可以使用该os模块清除屏幕。

import os 

os.system('clear')

在打印“进来”之前使用此代码,并在代码的开头导入 os,例如:

import os

print ('hello, welcome to our bar')
age = int(input("What is your age"))

if age < 21:
    print ('*kicks your ass out of bar*')
else:
    os.system('clear')
    print("come on in") 
于 2021-04-02T04:55:20.263 回答
0

你只需要使用ospython 模块。

虽然有两种方法可以做到这一点,即使用函数或方法以及直接使用os模块。

  • 使用函数或方法

import os

print ('hello, welcome to our bar \n')
age = int(input("What is your age \n"))

def ClearScreen():
   # for mac and linux(here, os.name is 'posix')
   if os.name == 'posix':
      _ = os.system('clear')
   else:
      # for windows platfrom
      _ = os.system('cls')

if age < 21:
    ClearScreen()
    print ('*kicks your ass out of bar* \n')
     
else:
    ClearScreen()
    print("come on in \n")

  • 直接使用os模块。

import os

print ('hello, welcome to our bar \n')
age = int(input("What is your age \n"))

if age < 21:
    if os.name == 'posix':
      _ = os.system('clear')
    else:
      # for windows platfrom
      _ = os.system('cls')
    print ('*kicks your ass out of bar* \n')
     
else:
    if os.name == 'posix':
      _ = os.system('clear')
    else:
      # for windows platfrom
      _ = os.system('cls')
    print("come on in \n")

于 2021-04-02T05:15:11.090 回答