我正在使用的Python教程略有过时,但我决定继续使用最新版本的Python来练习调试.有时书中的代码中有一些东西我在更新的Python中已经改变了,我不确定这是否是其中之一.
在修复程序以便打印更长的阶乘值时,它使用long int来解决问题.原始代码如下:
#factorial.py
# Program to compute the factorial of a number
# Illustrates for loop with an accumulator
def main():
n = input("Please enter a whole number: ")
fact = 1
for factor in range(int(n), 0, -1):
fact = fact * factor
print("The factorial of ", n, " is ", fact)
main()
Run Code Online (Sandbox Code Playgroud)
long int版本如下:
#factorial.py
# Program to compute the factorial of a number
# Illustrates for loop with an accumulator
def main():
n = input("Please enter a …Run Code Online (Sandbox Code Playgroud) 在我正在使用的Python教程书中,我输入了一个同时分配的示例.我在运行程序时得到了前面提到的ValueError,并且无法找出原因.
这是代码:
#avg2.py
#A simple program to average two exam scores
#Illustrates use of multiple input
def main():
print("This program computes the average of two exam scores.")
score1, score2 = input("Enter two scores separated by a comma: ")
average = (int(score1) + int(score2)) / 2.0
print("The average of the scores is:", average)
main()
Run Code Online (Sandbox Code Playgroud)
这是输出.
>>> import avg2
This program computes the average of two exam scores.
Enter two scores separated by a comma: 69, 87
Traceback (most recent …Run Code Online (Sandbox Code Playgroud) 我尝试在网站上搜索类似的问题,但似乎没有一个我的确存在两难.
有关Python的教程,我编写了一个将摄氏温度转换为华氏温度的程序.当我在IDLE shell中运行它时,程序可以正常工作,但会返回错误.这是程序本身的代码:
#convert.py
#converts Celsius temperatures to Farenheit
def main():
celsius = input("Type the temperature in degrees Celsius: ")
farenheit = ((9/5)* int(celsius)) + 32
print(farenheit)
main();
Run Code Online (Sandbox Code Playgroud)
这是错误.
>>> import convert.py
Type the temperature in degrees Celsius: 59
138.2
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import …Run Code Online (Sandbox Code Playgroud)