簡體   English   中英

使用余數模運算符花費的Python時間

[英]Python Time Spent using Remainder Modulo Operator

我一直堅持創建一個人花在回答問題上的程序。

對於第一個問題,他們花了26分鍾。 對於另外三個問題,他們花了12分鍾。

我要在屏幕上顯示的輸出是:

  • 花費的總分鍾數。
  • 花費的小時和分鍾數為整數。

這是我當前的代碼,無法計算出剩余函數的使用方式。

理想情況下,我試圖找到一種無需使用if語句就可以做到的方法。

answer = 26
additional_answers = 12
hour = 60
minutes_spent = answer + additional_answers

print(answer + additional_answers, " minutes spent")

if hour <= 60:
    print(0, "hours", + minutes_spent)

使用整數除法獲得小時數,並使用模數獲得分鍾部分。

total = answer + additional_answers
hours = total // 60
minutes = total % 60
print(total, " minutes spent")
print(hours, " hours, ", minutes, " minutes")

還有一個函數divmod一次執行兩個操作:

hours, minutes = divmod(total, 60)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM