[英]Need to figure out why my Python Program isn't working (sorting - alphabetizing)
The instructions were to: Write a Python program that reads in an two or more strings from the command line and displays them sorted alphabetically. 指示如下:编写一个Python程序,该程序从命令行读取两个或多个字符串,并按字母顺序显示它们。 Basic Requirements - Validate inputs - Proper error handling - Sorts two or or more words passed as program arguments - Correct documentation - Proper documentation mean that the top of the file should contain your name, the program's name, the purpose of the program and an example invocation of the program.
基本要求-验证输入-正确的错误处理-对作为程序参数传递的两个或多个单词进行排序-正确的文档-正确的文档意味着文件顶部应包含您的姓名,程序名称,程序的用途和示例程序的调用。 Also documentation complex or confusing lines in your code.
还要在代码中记录复杂或混乱的行。 However, there should not be too many of those.
但是,其中不应有太多。
My code: 我的代码:
# mySorter.py.
# This program reads two or more strings as program
# arguments and displays them sorted.
# Program will sort alphabetically the words from a string provided by the
user
# Take input from the user
my_str = input("Enter a string: ")
# breakdown the string into a list of words
words = my_str.split()
# sort the list
words.sort()
for word in words:
print(word)
if len(my_str.split()) < 2:
print("Please provide more than one word.")
Apparently, I am doing something wrong. 显然,我做错了。 I was told that This program does not meet the requirements.
有人告诉我该程序不符合要求。 It has to read arguments passed to the program when the program is started.Any suggestions?
它必须在程序启动时读取传递给程序的参数。有什么建议吗? Thanks.
谢谢。
# mySorter.py.
# This program reads two or more strings as program
# arguments and displays them sorted.
# Program will sort alphabetically the words from a string provided by the user
# Simply run this program as follows:
# python your_script.py "hello user"
import argparse
argparser = argparse.ArgumentParser()
argparser.add_argument('my_str',help = 'User string')
args = argparser.parse_args()
if args.my_str:
my_str = args.my_str
# breakdown the string into a list of words
words = my_str.split()
if len(words) <= 1:
print("Please provide more than one word.")
else:
# sort the list
words.sort()
for word in words:
print(word)
else:
print('No words to sort!')
print('Try the following: python my_script.py "hello world"')
The requirement of "It has to read arguments passed to the program when the program is started." “必须在程序启动时读取传递给程序的参数”的要求。 to me, tells me you need to use argparser to send a string without being prompted by an input().
对我来说,告诉我您需要使用argparser来发送字符串,而不会受到input()的提示。 Which explains why you aren't meeting it, since you aren't currently doing this :-)
这就解释了为什么您不满足它的原因,因为您当前没有这样做:-)
I was told that This program does not meet the requirements.
有人告诉我该程序不符合要求。 It has to read arguments passed to the program when the program is started
启动程序时,它必须读取传递给程序的参数
Instead of using input()
to prompt the user for strings, I think they want you to use something like sys.argv
to get the list of input strings that you need to sort from the command line, eg python myscript.py str1 str2 str3
. 我认为他们不希望使用
input()
来提示用户输入字符串,而是希望您使用sys.argv
东西来获取需要从命令行进行排序的输入字符串的列表,例如python myscript.py str1 str2 str3
。
Slightly modified for python3 from https://www.tutorialspoint.com/python/python_command_line_arguments.htm , put the following into a "test.py" file: 从https://www.tutorialspoint.com/python/python_command_line_arguments.htm对python3进行了稍微修改,将以下内容放入“ test.py”文件中:
import sys
print('Number of arguments: {} arguments'.format(len(sys.argv)))
print('Argument List: {}'.format(sys.argv))
At the command line, do: 在命令行中,执行以下操作:
python test.py arg1 arg2 arg3
This produces the following result: 这将产生以下结果:
Number of arguments: 4 arguments.
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']
To get the arguments excluding the script name, you can do something like words = sys.argv[1:]
. 要获取不包括脚本名称的参数,您可以执行诸如
words = sys.argv[1:]
。 This will return a list of arguments starting with the second one. 这将从第二个参数开始返回参数列表。 You can then go ahead and sort your list as per your original program.
然后,您可以继续按照原始程序对列表进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.