0
我的書需要我到使用函數製作另一個程序,這個問題的關鍵是我需要使它更復雜一些,所以不是簡單的添加,而是嘗試解決一個非常簡單的物理用戶使用兩個函數(速度和加速度)給我的值的問題。第二功能程序。一些查詢
繼承人的程序的目的(sampledoc)
- Create a program that reads and prints this txt document
- Introduces the Program and the name of the script
Uses a function to solve a problem, make the function more complex
- User gives distance (x) and time (t)
- Program calculates velocity and acceleration
Creates a new txt document and writtes the results in it
- Prints the results of the problem directly from the new document.
而且繼承人的代碼:
from sys import argv
script, textf = argv; sampledoc = open(textf)
def velocity (x, t):
vel = (float(x))/(float(t))
return float(vel)
def acceleration (v, t):
accel = (float(v))/(float(t))
return float(accel)
print "Hello my name is TAR or as my creator called me %s" % script; print sampledoc.read(); sampledoc.close()
print "Results will be printed on a new text document, thanks for your preference"
x = float(raw_input("Please introduce the Distance")); t = float(raw_input("Please introduce the time:... "))
vel = velocity(x, t)
accel = acceleration (velocity, t)
results = 'ResultsP.txt'
new_file = open(results, 'w')
new_file.write(str(vel)); new_file.write(str(accel))
new_file.close()
new_file.open(results, 'r')
print new_file.read()
new_file.close()
我知道有什麼錯在這裏,某個地方,但我的大腦並沒有馬上工作,我想這與我試圖解決這個問題的方式有關,或者我在函數中使用的''float'',因爲我得到這個錯誤:
File "ex21Study.py", line 20, in <module>
accel = acceleration (velocity, t)
File "ex21Study.py", line 10, in acceleration
accel = (float(v))/(float(t))
TypeError: float() argument must be a string or a number
我搜索了這個,發現了一些關於將我的結果轉換爲float或str的類似問題的一些答案,但是,我嘗試了這兩種方法,結果並不好。
什麼是你看到錯誤的*全回溯*? –
風格提示:不要在Python中使用';'。它使你的代碼很難閱讀,並且很難調整/維護。唯一可以接受的是交互式翻譯中的快速片段。 – iCodez