想循环这个脚本吗?Python3

2024-05-08 20:07:54 发布

您现在位置:Python中文网/ 问答频道 /正文

import time
morse = {'a': '.-',     'b': '-...',   'c': '-.-.', 
        'd': '-..',    'e': '.',      'f': '..-.',
        'g': '--.',    'h': '....',   'i': '..',
        'j': '.---',   'k': '-.-',    'l': '.-..',
        'm': '--',     'n': '-.',     'o': '---',
        'p': '.--.',   'q': '--.-',   'r': '.-.',
        's': '...',    't': '-',      'u': '..-',
        'v': '...-',   'w': '.--',    'x': '-..-',
        'y': '-.--',   'z': '--..',

        '0': '-----',  '1': '.----',  '2': '..---',
        '3': '...--',  '4': '....-',  '5': '.....',
        '6': '-....',  '7': '--...',  '8': '---..',
        '9': '----.', ' ': '          '}
print ("""
================================================================================
                      English to Morse Code Translator
================================================================================""")
time.sleep(2)
print ("")
msg = input('''What would you like translated? please use lower-case letters.
:''')
print ("")
for i in msg:
    print(morse[i])

我想在msg输入之后循环它,所以它会在为我翻译第一件事之后询问我想要翻译什么。你知道吗


Tags: toimportyouinputmorsetimeenglishcode
1条回答
网友
1楼 · 发布于 2024-05-08 20:07:54
while True:  



    msg = input('''What would you like translated? please use lower-case letters.:''')
    print ("")
    for i in msg:
        print(morse[i])

为用户提供退出程序的机会:

while True:  



    msg = input('''What would you like translated? please use lower-case letters.:''')
    print ("")
    for i in msg:
        print(morse[i])
    userstatus = input('Would you like to go again?(y or n): ').lower().strip(' ')
    if userstatus not in('y', 'ye', 'yes'):
        sys.exit()

注意,您必须使用此示例导入sys系统出口()工作

相关问题 更多 >