def rydbergEquation(finalEnergyLevel, initialEnergyLevel):
rydbergConstant = 1.0974 * 10**7
finalEnergyTerm = 1 / (finalEnergyLevel**2)
initialEnergyTerm = 1 / (initialEnergyLevel**2)
variableTerm = finalEnergyTerm - initialEnergyTerm
variableTerm = rydbergConstant * variableTerm
unlambda = variableTerm**-1
return unlambda
print("Energy Level (2 to 1): ", rydbergEquation(1, 2))
print("Energy Level (3 to 1): ", rydbergEquation(1, 3))
print("Energy Level (4 to 1): ", rydbergEquation(1, 4))
print("Energy Level (5 to 1): ", rydbergEquation(1, 5))
print("Energy Level (6 to 1): ", rydbergEquation(1, 6))
print("Energy Level (7 to 1): ", rydbergEquation(1, 7))
print("Energy Level (3 to 2): ", rydbergEquation(2, 3))
print("Energy Level (4 to 2): ", rydbergEquation(2, 4))
print("Energy Level (5 to 2): ", rydbergEquation(2, 5))
print("Energy Level (6 to 2): ", rydbergEquation(2, 6))
print("Energy Level (7 to 2): ", rydbergEquation(2, 7))
print("Energy Level (4 to 3): ", rydbergEquation(3, 4))
print("Energy Level (5 to 3): ", rydbergEquation(3, 5))
print("Energy Level (6 to 3): ", rydbergEquation(3, 6))
print("Energy Level (7 to 3): ", rydbergEquation(3, 7))
print("Energy Level (5 to 4): ", rydbergEquation(4, 5))
print("Energy Level (6 to 4): ", rydbergEquation(4, 6))
print("Energy Level (7 to 4): ", rydbergEquation(4, 7))
print("Energy Level (6 to 5): ", rydbergEquation(5, 6))
print("Energy Level (7 to 5): ", rydbergEquation(5, 7))
print("Energy Level (7 to 6): ", rydbergEquation(6, 7))
我可以删除21条print
语句并创建一个for循环以输出给定值吗?我似乎找不到一种方法来循环该函数以获取不同能级变化的输出。
答案 0 :(得分:2)
您可以将itertools.combinations
用于此目的:
import itertools as it
for i, j in it.combinations(range(1, 8), r=2):
print(f"Energy Level ({j} to {i}): ", rydbergEquation(i, j))
答案 1 :(得分:2)
Itertools可能更好,但是在很小的情况下,您可以使用double for循环来获得:
for i in range(6):
for j in range(i+1, 7):
print(f"Energy Level ({i+1} to {j+1}): ", rydbergEquation(i+1, j+1))
以此替换所有打印语句。