编写一个接受整数值(n),重复(r)和计算的Python程序 [英] Write a Python program that accepts an integer value (n), repetition (r) and compute

查看:134
本文介绍了编写一个接受整数值(n),重复(r)和计算的Python程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sum1 = n + nn + nnn + ... n ^ r。

例如:5 + 55 + 555 = 615







Sum2 = n +(n * n)+(n * n * n)+ ... n ^ r。

例如:5 +(5 * 5)+(5 * 5 * 5)= 155



我试过的:



n = int(输入(输入数字n:))

temp = str(n)

t1 = temp + temp

t2 = temp + temp + temp

comp = n + int(t1)+ int(t2)

print(值为:,comp)

Sum1= n+nn+nnn+…n^r.
Eg:5+55+555=615



Sum2=n+(n*n)+(n*n*n)+…n^r.
Eg:5+(5*5)+(5*5*5)=155

What I have tried:

n=int(input("Enter a number n: "))
temp=str(n)
t1=temp+temp
t2=temp+temp+temp
comp=n+int(t1)+int(t2)
print("The value is:",comp)

推荐答案

Case 1:
Enter a number n: 5
The value is: 615
 
Case 2:
Enter a number n: 20
The value is: 204060


为什么要将整数转换为字符串来进行计算?您已使用 int 函数将输入作为数值获取。



你的程序应该首先接受n的值,作为整数。

它应该接受r的值并使用这两个用于执行计算的数字。

您的代码不够,并且不允许r的值。
Why are you converting an integer to a string to do the calculation? You already used the int function to get the input as a numeric value.

Your program should accept the value for n first, as an integer.
It should then accept the value for r and use those two numbers to perform the calculation.
Your code does not go far enough, and does not allow for the value of r.


这篇关于编写一个接受整数值(n),重复(r)和计算的Python程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆