简体   繁体   English

每次我在命令中输入视图时,我都无法弄清楚如何让它不说添加视图到列表中

[英]I cant figure out how to make it not say added view to list every time i type view into the command

I cant figure out how to make it not say added view to list every time i type view into the command.每次我在命令中输入视图时,我都无法弄清楚如何让它不说添加视图到列表中。

or any of the commands that i can type in.或我可以输入的任何命令。

I tried to make it so that if you dont type the commands but it doesnt work.我试图做到这一点,如果你不键入命令但它不起作用。

# Declaring the sorting var

sorting = []

# Sorting the list

def sort():
    sorting.remove('sort')
    print(' ')
    print('Your sorted list:')
    print(' ')
    print('-----------------------------------------------')
    sorting.sort()
    print(sorting)
    print('-----------------------------------------------')
    print(' ')

# Clearing the list

def clear():
    sorting.clear()
    print('-----------------------------------------------')
    print('cleared your list')
    print('-----------------------------------------------')
    print(' ')

# View the list without sorting

def view():
    sorting.remove('view')
    print('Your list:')
    print(' ')
    print('-----------------------------------------------')
    print(sorting)
    print('-----------------------------------------------')

# Quiting

def quiting():
    sorting.remove('quit')
    quit()

# Adding things to list and command functions

def append():
    sort_appending = input('type something to add it to list, type sort to sort your list, type clear to clear your list, type view to view your list not sorted type quit to quit: ')
    print(' ')
    if sort_appending != 'sort' or 'quit':
        sorting.append(sort_appending)
        print('-----------------------------------------------')
        print('Added', sort_appending, 'to your list')
        print('-----------------------------------------------')
        print(' ')
    if sort_appending == 'sort':
        sort()
    if sort_appending == 'clear':
        clear()
    if sort_appending == 'view':
        view()
    if sort_appending == 'quit':
        quiting()

# Main loop

run = True

while run == True:
    append()

If you dont understand me you could put it into your python IDLE and test it如果您不理解我,您可以将其放入您的 python IDLE 并进行测试

Please help me this is a problem i need help with.请帮助我这是我需要帮助的问题。

'view' is being interpreted as an "add an element" input by this check here:此处检查将'view'解释为“添加元素”输入:

    if sort_appending != 'sort' or 'quit':
        sorting.append(sort_appending)

As an easy fix I'd suggest restructuring this as an if...elif chain so that you only do one thing with any given piece of input:作为一个简单的解决方法,我建议将其重组为if...elif链,以便您只对任何给定的输入做一件事:

    if sort_appending == 'sort':
        sort()
    elif sort_appending == 'clear':
        clear()
    elif sort_appending == 'view':
        view()
    elif sort_appending == 'quit':
        quiting()
    else:  # any other input is implicitly treated as an 'add'
        sorting.append(sort_appending)
        print('-----------------------------------------------')
        print('Added', sort_appending, 'to your list')
        print('-----------------------------------------------')
        print(' ')

This prevents you from having to duplicate each "command" in that first check (and hitting bugs like this when you add a new command in one place but forget to add it there)!这可以防止您在第一次检查中复制每个“命令”(当您在一个地方添加新命令但忘记在那里添加时遇到这样的错误)!

if sort_appending not in ['sort','quit','view']:
    sorting.append(sort_appending)
    print('-----------------------------------------------')
    print('Added', sort_appending, 'to your list')
    print('-----------------------------------------------')
    print(' ')

You already have the answer.你已经有了答案。 Just add 'view' to this too!只需在其中添加“视图”!

Here is an alternate solution这是一个替代解决方案

def append():
    sort_appending = input('type something to add it to list, type sort to sort your list, type clear to clear your list, type view to view your list not sorted type quit to quit: \n')
    print(' ') 
    if sort_appending == 'sort':
        sort()
    elif sort_appending == 'clear':
        clear()
    elif sort_appending == 'view':
        view()
    elif sort_appending == 'quit':
        quiting()
    else:
        sorting.append(sort_appending)
        print('-----------------------------------------------')
        print('Added', sort_appending, 'to your list')
        print('-----------------------------------------------')
        print(' ')

Also change the following还要更改以下

def view():
    #sorting.remove('view')
    print('Your list:')
    print(' ')
    print('-----------------------------------------------')
    print(sorting)
    print('-----------------------------------------------')

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM