我原来是一名C程序员.我已经看过许多技巧和"黑客"来阅读许多不同的论点.
Python程序员可以通过哪些方式实现这一目标?
是否可以先运行程序,然后在命令行中等待用户的输入.例如
Run...
Process...
Input from the user(in command line form)...
Process...
Run Code Online (Sandbox Code Playgroud) 我在python中实现了一个小命令行工具,需要向用户询问几个问题.我用
raw_input('Are you male or female?')
Run Code Online (Sandbox Code Playgroud)
每时每刻.现在我希望能够处理愚蠢的用户(或者那些懒得阅读/记住文档的人),所以我需要检查答案是否有意义.
gender = ''
while gender not in ['male', 'female']:
gender = raw_input('Are you male or female?')
Run Code Online (Sandbox Code Playgroud)
我想知道是否存在类似argparse的东西可以自动解决这个问题
import inputparse
gender = inputparse.get_input(prompt='Are you male or female?', type=str, possible_input=['male', 'female'])
Run Code Online (Sandbox Code Playgroud)
并会照顾自动检查等?
我在Python中编写了一个函数,它提示用户给出两个数字并添加它们.它还会提示用户输入城市并进行打印.出于某种原因,当我在shell中运行它时,在进入城市后我得到"名称未定义".
def func_add(num1, num2):
a = input("your city")
print a
return num1 + num2
Run Code Online (Sandbox Code Playgroud) 我正在尝试比较用户收到的输入,检查输入是否实际上是一个数字.这是我到目前为止所拥有的:
numstring = input("Choose a number between 1 and 10")
Run Code Online (Sandbox Code Playgroud)
然后比较我在这样的方法中有它:
def check(a):
if int(a) == int:
print("It's a number!")
else:
print("It's not a number!")
Run Code Online (Sandbox Code Playgroud)
它总是打印出它不是一个数字,即使它是.
这个计划的目标:我正在做一个猜谜游戏:
import random
numstring = input("Choose a number between 1 and 10")
def check(a):
if int(a) == int:
print("It's a number!")
else:
print("It's not a number!")
if abs(int(a)) < 1:
print("You chose a number less than one!")
elif abs(int(a)) > 10:
print("You chose a number more than ten!")
else:
randomnum = random.randint(1, 10)
randomnumstring …Run Code Online (Sandbox Code Playgroud) 我有一些代码可以操作当前在脚本中硬编码的文件的数据。我希望能够提示用户选择输入文件,而不必对其进行硬编码。这是我需要输入的内容。我希望用户能够选择文件,而不是总是使用 myfile.txt:
with open('myfile.txt', 'rU') as input_file:
Run Code Online (Sandbox Code Playgroud) 我有以下代码,它应该询问用户 2 文件名。我在第二个函数中的 input() 出现错误,但在第一个函数中没有,我不明白......这是错误:
output = getOutputFile() File "splitRAW.py", line 22, in getOutputFile fileName = input("\t=> ") TypeError: 'str' object is not callable
# Loops until an existing file has been found
def getInputFile():
print("Which file do you want to split ?")
fileName = input("\t=> ")
while 1:
try:
file = open(fileName, "r")
print("Existing file, let's continue !")
return(fileName)
except IOError:
print("No such existing file...")
print("Which file do you want to split ?")
fileName = input("\t=> ")
# Gets …Run Code Online (Sandbox Code Playgroud) 我正在制作一个简单的基于文本的游戏,但我什至无法通过第二行代码。到目前为止我的代码如下所示:
print("Welcome To City Text! First You Must Name Your City.")
Run Code Online (Sandbox Code Playgroud)
然后这个人应该输入一个名字,shell 将打印“欢迎来到“城镇名称”城市”问题是我不知道该怎么做。有谁知道如何做到这一点?