当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Re-run a code from a particular point

问题描述:

I don't know how to re-run a certain piece of my code:

print('Welcome to your currency converter\n Please choose your starting currency from the four options:')

currency1=input('(GDP) = Pound Sterling £ \n(EUR) = Euro € \n(USD) = US Dollar ($) \n(JPY)= Japanese Yen ¥\n').lower()

if currency1!='gdp' or 'eur' or 'usd' or 'jpy':

print('Sorry not accepted try again')

When the code prints Sorry not accepted try again I want to start the code again. How do I do this?

网友答案:

A better way to do this than the other answers reccomend, is to put the condition in the while statement like this:

print('Welcome to your currency converter\n Please choose your starting currency from the four options:')
currency1 = input('(GDP) = Pound Sterling £ \n(EUR) = Euro € \n(USD) = US Dollar ($) \n(JPY)= Japanese Yen ¥\n').lower()

while currency1 not in ['gdp', 'eur', 'usd', 'jpy']:
    print('Sorry not accepted try again')
    currency1 = input('(GDP) = Pound Sterling £ \n(EUR) = Euro € \n(USD) = US Dollar ($) \n(JPY)= Japanese Yen ¥\n').lower()
网友答案:

The official FAQ covers three options for this, although you'd probably never find it because it's under the question Why can't I use an assignment in an expression:


First, there's while True:

print('Welcome to your currency converter\n Please choose your starting currency from the four options:')
while True:
    currency1=input('(GDP) = Pound Sterling £ \n(EUR) = Euro € \n(USD) = US Dollar ($) \n(JPY)= Japanese Yen ¥\n').lower()
    if currency1 not in ['gdp','eur','usd','jpy']:
        print('Sorry not accepted try again')
    else:
        break        

This may seem odd to diehard C programmers, who were taught that break (like early return and similar features) is bad. But Python isn't C. (Not to mention that nowadays, even MISRA recommends using break in new C99 code…) You can avoid the break by using while not done: and setting done = True instead of break, but there is no advantage to that, and it just makes your code longer and more complicated.


Next, there's while <condition>:

print('Welcome to your currency converter\n Please choose your starting currency from the four options:')
currency1 = input('(GDP) = Pound Sterling £ \n(EUR) = Euro € \n(USD) = US Dollar ($) \n(JPY)= Japanese Yen ¥\n').lower()

while currency1 not in ['gdp', 'eur', 'usd', 'jpy']:
    print('Sorry not accepted try again')
    currency1 = input('(GDP) = Pound Sterling £ \n(EUR) = Euro € \n(USD) = US Dollar ($) \n(JPY)= Japanese Yen ¥\n').lower()

As the FAQ says, this "seems attractive but is generally less robust":

The problem with this is that if you change your mind about exactly how you get the next line (e.g. you want to change it into sys.stdin.readline()) you have to remember to change two places in your program – the second occurrence is hidden at the bottom of the loop.


Finally, as the FAQ says, "the best approach is to use iterators, making it possible to loop through objects using the for statement".

That's great when you already have an iterator, or can build one trivially. But when you don't, it may add more complexity than you save. And I think that's the case here. For example, this is hardly simpler than the other options:

def get_currency():
    currency1=input('(GDP) = Pound Sterling £ \n(EUR) = Euro € \n(USD) = US Dollar ($) \n(JPY)= Japanese Yen ¥\n').lower()
    if currency1 in ['gdp','eur','usd','jpy']:
        return currency
    else:
        print('Sorry not accepted try again')
for currency1 in iter(get_currency, None):
    do_stuff(currency1)
网友答案:

You want to use a while loop, to keep running the code "while" it's invalid:

while True:
    currency1 = input('(GDP) = Pound Sterling £ \n(EUR) = Euro € \n(USD) = US Dollar ($) \n(JPY)= Japanese Yen ¥\n').lower()
    if currency1 not in ('gdp', 'eur', 'usd', 'jpy'):
        print('Sorry not accepted try again')
    else:
        break

Note: currency1!='gdp'or'eur'or'usd'or'jpy' doesn't do what you think it does. Use the in method as above

Edited per abarnert's comment

分享给朋友:
您可能感兴趣的文章:
随机阅读: