我是初学者
上下文:我上个月刚开始编码。我是一名笔译和口译专业的学生,有一个论文项目。我选择为翻译人员创建一个辅助程序,使在线查找单词等变得更容易。我从StackOverflow和类似的网站中受益匪浅,创建了一些运行良好的代码——至少对于初学者来说是如此
我需要的是一种将我的小代码组合成一个更大的程序的方法,在这个程序中,用户可以选择他们想要使用的项目功能。例如,我有一个计时器,可以通知用户在给定的时间间隔内休息,还有一个用于机器翻译的集成API
我在网上查找类似的问题,但它们不包括根据用户需求工作的命令。我脑子里想的是一个程序,它的工作原理如下:
当您运行代码时,它会询问您“您想使用哪个工具?翻译还是计时器?”并根据用户需求运行相应的命令。以下是我的两个代码示例:第一个是我的机器翻译API,第二个是我的通知计时器
import requests
print('Welcome to the translation tool. Below are some language codes for you to use:')
print('English -> en, Turkish -> tr, Spanish -> es, French -> fr, Russian -> ru, Chinese -> zh.')
print('***You can type "e" into the source word box to exit the program.***')
sourcelang = str(input('Which language would you like to translate FROM?: '))
targetlang = str(input('Which language would you like to translate TO?: '))
while 1==1:
url = "https://systran-systran-platform-for-language-processing-v1.p.rapidapi.com/translation/text/translate"
word = str(input('Which word would you like to look up?: '))
querystring = {"source":sourcelang, "target":targetlang, "input":word}
headers = {
'x-rapidapi-key': "8a96426f46msh7c7b8957d8b6d49p12c046jsnf7904623bf34",
'x-rapidapi-host': "systran-systran-platform-for-language-processing-v1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
if word == str('e'):
print ('Thanks for using this software. Have a good one.')
break
及
import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
import time
userinput = input('Specify time intervals in minutes: ')
print('Timer started, will notify to take a break in specified intervals.')
while 1 == 1:
notifString = """
<toast>
<visual>
<binding template='ToastGeneric'>
<text>Time to Get Up</text>
<text>Stretch Your Legs</text>
</binding>
</visual>
</toast>
"""
notifTime = float(userinput)
notifManager = notifications.ToastNotificationManager
notif = notifManager.create_toast_notifier()
xmlNotif = dom.XmlDocument()
xmlNotif.load_xml(notifString)
def givenotification(t) :
time.sleep(t*60)
notif.show(notifications.ToastNotification(xmlNotif))
givenotification(notifTime)
如果我的问题太模糊或者你需要更多的细节,请告诉我。提前谢谢
附言:如果能为我的代码和程序提供更多帮助,我将不胜感激:)
您可以在
Python2
中使用execfile('filename.py')
或
您可以在
Python3
中使用exec(open('filename.py').read())
或者,您可以简单地将另一个翻译和计时器文件作为模块导入,并在
if else
块中执行它们的函数您可以将这两个工具包装到它们自己的方法中,然后在程序开始时为用户提供选择(在本例中为
main()
):我还将
if word == str('e):
向上移动,这样您就不必等待http请求就可以退出程序您可以使用操作系统模块运行文件。您可以使用该块和if-else块检查用户输入并运行所需的文件。以下是一个例子:
相关问题 更多 >
编程相关推荐