我需要为此连续输入,但是当我使用while循环时,它会中断

时间:2019-06-09 20:16:36

标签: python

我尝试在main_function中的打印后使用while循环,但是输入仅转到“您要购买什么”,然后中断。任何提示或解决方案?

我试图做这样的输入功能: def inp():     全球使用     使用= input('/ >>')     inp() 在将use变量设为全局后,所有这些函数都在我的main_func函数中。这是python 3.7.1,我只是想不出为什么它对第一个问题没有更深入的了解。


import sys
import os


inventory = []
stock = ['spoon', 'fork']




def main_func():
    print('Welcome to the store, some ground rules before we begin:\n')
    print('1)No running, plz\n2)No stealing, your bad if you steal!!\nEnjoy')
    print('Commands: Sell, Buy, Exit')

    running = True
    while running == True:
        inp = input("/>> ")



        if inp == 'Buy' or inp == 'B' or inp == 'buy':
            print('What would you like to buy?: ')
            if inp == 'spoon' or inp == 'Spoon':  
                stock.remove('spoon')
                inventory.append('spoon')
                if 'spoon' not in stock and 'spoon' in inventory:
                    print('Successfully bought a spoon!')
            elif inp == 'fork' or inp == 'Fork':
                stock.remove('fork')
                incentory.append('fork')
                if 'fork' not in stock and 'fork' in inventory:
                    print('Successfully bought a fork')
        elif inp == 'Sell' or inp == 'sell' or inp == 'S':
            print('What would you like to sell?: ')
            if inp == 'spoon' or inp == 'Spoon' or inp == 'S':
                if 'spoon' not in inventory:
                    print("You can't sell something you don't have")
                if 'spoon' in inventory:
                    inventory.remove('spoon')
                    stock.append('spoon')
                    print("Successfully sold "+inp+".")
            elif inp == 'fork' or inp == 'Fork' or inp == 'F':
                if 'fork' not in inventory:
                    print("You can't sell something you don't have")
                if 'fork' in inventory:
                    inventory.remove('fork')
                    stock.append('fork')
                    print('Successfully sold Fork..')
        elif inp == "Exit" or inp == 'exit' or inp == 'quit':
            sys.exit()

main_func()

'''我知道代码的结构不良且性能很差,我只想尝试一些东西'''

没有弹出错误消息,但是在询问“您想购买什么”之后,我输入了其中一项,但它什么也没做,仍然可以输入,但对我放入的项目没有反应。例如:

“您想购买什么?:” / >>勺子#按Enter# / >>#它再次获得输入,却什么也不做#

2 个答案:

答案 0 :(得分:0)

您在提供的代码中仅从标准输入中读取一次,即该行

inp = input("/>>")

在此之后,您将打印消息“您想购买什么?:”,而不是创建提示。

只要想从标准输入中读取内容,就应该使用input方法。

inp = input("What would you like to buy?: ")

答案 1 :(得分:0)

没有错误消息,因为没有错误。该代码正是您实现的功能。

在第一次输入- @Effect() pollDataForCollection$ = this.collectionEventsActions$.pipe( ofType( CollectionActions.onCountdownRestarted.type ) , withLatestFrom( this.featuresStore$.select(collectionSelectors.selectAllFavourites) ,this.featuresStore$.select(collectionSelectors.selectExpandedFavourites) ) , switchMap(([action, faves, expandedDetailIds]) => { const ids: number[] = faves.map(fave => fave.id); return this.apiService.getParticipantsById(ids); ), switchMap(participants => { //for each participant map the detail api observable in an array const obs = participants.map(p => this.apiService.getParticipantDetailResults(participant.id)); return forkJoin(obs); }), map(joined => { // result from forkJoin will be an array of the api response in the order you added the observables. Also forkJoin only emit the value once for each of the observable it has the response; so it will wait for the response to comeback for the details for each participant //now you can do whatever you want to do with this array // may be parse it and send the success action to the store }) 之后,条件

Buy

得到满足-但没有其他子条件。这样就达到了点

if inp == 'Buy' or inp == 'B' or inp == 'buy':
        print('What would you like to buy?: ')
再次在inp = input("/>> ") 循环中

。您输入while。这会将变量spooninp更改为Buy。所以这一次,

spoon

if inp == 'Buy' or inp == 'B' or inp == 'buy':

elif inp == 'Sell' or inp == 'sell' or inp == 'S':

很满意,所以elif inp == "Exit" or inp == 'exit' or inp == 'quit': 循环再次要求输入。