-2

In Python I want to see if I can print the final value of a variable, for example, X, y times as a string. If program is:

>>>y=int(input("Enter no. of times x should be printed"))
   z=int(input("Enter value of x"))       
   x=0
for a in range(1,z+1):
    x=x+1
    print(value of x*y times as a string)

Can I print the value of x which is z=4, therefore x is also 4, y times(eg.y=4) like:4444

(It is not a duplicate of question"Python - Print a string a certain number of times" as it is not simply printing an inputted string a number of times. I am trying to find how to print the end result of a loop a no. of times)

  • What is `a` used for? Why a loop? Where is `x` defined initially? This is super confusing. – deceze Jan 08 '16 at 16:00
  • 3
    ... Your computer science teacher doesn't think it's possible to write a program that prints "4444"? Has he ever used a computer before? – Kevin Jan 08 '16 at 16:00
  • 2
    @Kevin no offense to OP, but we should not leave out the possibility that there's a sizeable mismatch between the question asked and the answer recieved. – Nelewout Jan 08 '16 at 16:06
  • @deceze. Sorry forgot to add x=0,and a is used to provide a random value for x so the value can be printed. It is not a duplicate question as . Reason given in edited version of question. – Daniel Moon Jan 10 '16 at 14:57

3 Answers3

0
y=4
for a in range(1,5):
    x=x+1
print(str(x) * y)
dtanders
  • 1,835
  • 11
  • 13
0

By default, the input function accepts provided input as string.

So, basically, accepting the number as string itself, and multiplying it by number of times should do the job.

Similarly, creating a string using str(y) should also provide the same result.

For example

print y * z

where y is the number (as string) to be displayed z times.

aliasm2k
  • 883
  • 6
  • 12
0

The following program appears to do as you requested:

def main():
    x, y = 0, 4
    for _ in range(1, 5):
        x += 1
    print(str(x) * y)

if __name__ == '__main__':
    main()
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117