简体   繁体   English

循环的Python问题

[英]Python Issue with Loops

I have designed a code which will take a 'number' as an input from the user. 我设计了一个代码,它将一个“数字”作为用户的输入。

The number will be used to make a... 该号码将用于制作...

  1. numerator = (3*number) - 2
  2. and a denominator, which will be denominator = (4*n) + 1 . 分母,这将是denominator = (4*n) + 1

The code will also allow the user to choose how many times they want this sequence to go on after which the sum of all the fractions will be totaled and displayed. 该代码还将允许用户选择他们希望该序列继续执行多少次,之后将总计并显示所有分数的总和。

Here is the Code I have: 这是我的代码:

l=int(input("How many times do you repeat this sequence?: "))
n=int(input("Enter a base number: "))
n1=n
n2=n
total=0
s = ''
def calculate(l,n,n1,n2,total,s):
    for j in range(l):
        s += "{}/{} + ".format(3*n1-2, 4*n2+1)
        n1=n+n1
        n2=n+n2
        total=(((n*3)-2)/((4*n)+1))+total
    print(s)
    print(total)

calculate(l, n, n1, n2, total, s)

Now here are the two errors that I receive when I get the output for this code for example: 现在这是例如当我获得此代码的输出时收到的两个错误:

How many times do you repeat this sequence?: 2
Enter a base number: 1
1/5 + 4/9 + 
0.4

The two Issues: 两个问题:

  1. Since 4/9 is the last fraction, is there a way to get rid of that "+" addition sign at the end, because it just points to a blank space.. 由于4/9是最后一个分数,因此有一种方法可以消除最后的“ +”加号,因为它仅指向空格。
  2. The total for the two fractions shows to be 0.4 which is incorrect, the total sum should be 1/5 + 4/9 = 0.2 + 0.44 = 0.64 , I am unsure where I went astray when inputting my total sum formula above. 这两个分数的总和显示为0.4 ,这是不正确的,总和应为1/5 + 4/9 = 0.2 + 0.44 = 0.64 ,我不确定在上面输入我的总和公式时会误入歧途。

Any suggestions/comments would be appreciated! 任何建议/意见将不胜感激!

A cheap way of removing the + would be to simply cut off the last character in the string: str[:-1] . 删除+一种便宜方法是简单地切断字符串中的最后一个字符: str[:-1]

As far a issue 2 goes, it looks like you want to use n1 and n2 instead of n. 到目前为止,问题2似乎要使用n1和n2而不是n。

As of now, you're getting 1/5(.2) + 1/5(.2) = .4 截至目前,您获得的是1/5(.2)+ 1/5(.2)= 0.4

  1. Instead of concatening a string like that, collect all the parts in a list and then join the items on the plus sign: 与其连接这样的字符串,不如收集一个列表中的所有部分,然后将加号上的各项连接起来:

     s = [] s.append('{}/{}'.format(1, 5)) s.append('{}/{}'.format(4, 9)) print(' + '.join(s)) # 1/5 + 4/9 
  2. I'm not really sure what you are doing but if you want to get the sum of the fractions you print, you should just make sure that you calculate those individual fractions in the same way. 我不太确定自己在做什么,但是如果要获取打印分数的总和,则应确保以相同的方式计算这些分数。 So instead of incrementing n1 and n2 first before calculating the sum, calculate the sum in the same way you did for the fraction output and only afterwards change those variables: 因此,与其在计算总和之前先递增n1n2n2以与分数输出相同的方式计算总和,然后再更改这些变量:

     s.append("{}/{}".format(3 * n1 - 2, 4 * n2 + 1)) total += (3 * n1 - 2) / (4 * n2 + 1) n1 += n n2 += n 

I dont know python but you could do the following to correct your logical errors. 我不了解python,但是您可以执行以下操作来纠正逻辑错误。

  1. to remove the '+' at the end, you can do something like below, 要删除最后的“ +”号,您可以执行以下操作,

if j = l (implies last fraction) dont include + else include + 如果j = l(表示最后一个分数),则不包含+,否则包含+

  1. While calculating total you are using 'n' value which always remains as your input value total=(((n*3)-2)/((4*n)+1))+total Here use n1 or n2 total=(((n1*3)-2)/((4*n2)+1))+total 在计算总数时,您使用的是'n'值,该值始终保留为输入值total =((((n * 3)-2)/((4 * n)+1))+ total在这里使用n1或n2 total =( ((N1 * 3)-2)/((4 * N 2)+1))+总

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM