ですから、これは宿題であることを認めますが、私はあなた方全員にそれをしてくれるように頼んでいるのではなく、ただいくつかのガイダンスを探しています。時間を1つの文字列でHours:Minutes(2:30)形式で受け入れ、時間を分単位で出力するPythonプログラムを作成する必要があります。(つまり、2時間30分= 150分)
文字列入力のいくつかの制限を解決する必要があります。
- 数字とコロンのみを使用するようにしてください
- 5文字のみを受け入れることができることを確認してください(##:##)
- 真ん中の文字がコロンであることを確認してください(つまり、数字が正しい順序になっている)
- そして、4:35のような時間が入力された場合、ゼロが自動的に前に追加されることを確認してください
これについては後で作業します—今のところ、入力から得られる数学に取り組むことにしました。
文字列を時間と分という2つの部分にスライスすることは私にとって理にかなっています。次に、時間数に60を掛け、それらを既存の分に加算して、合計分数を取得しました。ただし、現在、02:45のような時間に入ると、02020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020
ここで何がうまくいかないのでしょうか?明確にするために、これは宿題であり、入力の制限を自分で解決したいので、この数学の問題を乗り越える手助けが必要です。
#Henry Quinn - Python Advanced 4.0 Hours and Minutes
import re
print "This program takes an input of time in hours and minutes and outputs the amount of minutes."
count = 0
#I still need to work out while loop
#Supposed to make sure that a time is entered correctly, or error out
while (count <1):
time = raw_input("Please enter the duration of time (ex: 2:15 or 12:30): ")
if not re.match("^[0-9, :]*$", time):
print "Sorry, you're only allowed to use the numbers 0-9."
elif len(time) > 5:
print "Sorry, only five characters max allowed."
#MAKE THIS CHECK FOR A COLON
#elif
#elif
else:
count = count + 1
#If time = 12:45, hours should be equal to 12, and minutes should be equal to 45
hours = time[:2]
minutes = time[3:]
#Should convert hours to minutes
newhours = hours * 60
#Should make total amount of minutes
totalminutes = newhours + minutes
print "The total amount of elapsed minutes is %s" % (totalminutes)
raw_input("Please press Enter to terminate the program.")