简体   繁体   English

需要弄清楚为什么我的Python程序不起作用(排序-按字母顺序排列)

[英]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.

相关问题 无法弄清楚为什么我的 Scrapy 脚本不起作用 - Can't figure out why my Scrapy script isn't working 我不知道为什么我的makemigrations无法在Django中工作,它显示以下错误 - I can't figure out why my makemigrations isn't working in django,its showing following error 我不知道为什么我的 web 抓取代码不起作用 - I can't figure out why my web scraping code isn't working 无法弄清楚为什么追加到列表不起作用 - Can't figure out why appending to list isn't working 我不明白为什么我的 python 代码没有以英里为单位返回答案 - I cant figure out why my python code isn't returning the answer in miles 试图找出为什么某些输出不起作用 - Trying to figure out why some of the output isn't working 试图为我的 python 类完成这个程序,但无法弄清楚为什么我会收到 TypeError - Trying to finish this program for my python class, but can't figure out why I'm receiving a TypeError 在我的代码中没有得到我需要的输出,我不知道为什么。 Python - Not getting the output I need in my code and I can't figure out why. Python Python 程序挂起,不知道为什么 - Python program hangs, can't figure out why Python-我的部分代码不起作用,我不知道为什么 - Python- Parts of my code aren't working and I can't figure out why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM