我是Python的新手,被要求创建一个程序,该程序将输入作为非负整数n,然后使用连续分数的前n + 1个项来计算e的近似值:
我试图破译这个问题,但无法完全理解它所问的一切。我不是在寻找确切的答案,而是希望有一个例子可以对我有所帮助。
这是确切的问题,
下面是我之前对连续分数所做的代码。
import math
# Get x from user
x = float(input("Enter x = "))
# Calculate initial variables and print
a0 = x//1
r0 = x-a0
print("a0 =", a0, "\tr0 =", r0)
# Calculate ai and ri for i = 1,2,3 and print results
a1 = 1/r0//1
r1 = 1/r0 - a1
print("a1 =", a1, "\tr1 =", r1)
a2 = 1/r1//1
r2 = 1/r1 - a2
print("a2 =", a2, "\tr2 =", r2)
a3 = 1/r2//1 …Run Code Online (Sandbox Code Playgroud) 我试图显示一个随机数组,同时也显示该数组的总和,但我无法让它工作,我知道如何使用for循环,但我被指示做一个while循环而不是任何想法?
private void SumOfArray() {
myWindow.clearOut();
int total = 0;
int[] a = new int[4];
int i;
i = 0;
while (i < a.length) {
i++;
a[i] = 1 + (int) (Math.random() * 10);
}
i = 0;
while (i < a.length) {
i++;
myWindow.writeOutLine(a[i]);
}
while (i < a.length) {
total += a[i];
i++;
}
myWindow.writeOutLine(total);
}
Run Code Online (Sandbox Code Playgroud) 我试图获得正确的输入,但当输入为假时,我希望它不断地再次询问,直到输出正确,我该如何正确地做到这一点?
x = input("Enter your string")
if set(x).issubset({'m', 'u', 'i'}):
print("true")
else:
print("false")
x = input("Enter your string")
Run Code Online (Sandbox Code Playgroud) 我只是习惯了如果在python中使用if语句但是我在尝试让我的工作时遇到了一些麻烦,到底发生了什么?
x = input("Enter your string")
while not set(x).issubset({'m', 'u', 'i'}):
print("false")
x = input("Enter your string")
else:
print("Your String is " + x)
Question = int(input("Which rule would you like to apply? enter numbers 1-4: "))
if Question is 1:
print("hello")
'''issue arises in the else area below'''
else if Question is not 1:
print("other")
Run Code Online (Sandbox Code Playgroud)