Home > Software engineering >  Why am I not getting 333 in Python
Why am I not getting 333 in Python

Time:12-22

num=int(input("what is your number"))
total=int(input("how many times do you want this number to appear"))
new_num=0
for i in range (total):
    new_num=num*10
    new_num=new_num num
print (new_num)

I keep getting 33, even when i changed my range to (total 1). What must I do to get 333

CodePudding user response:

When you have a problem like this use a debugger (python includes one in pdb) to step through the code and see how variables change.

In simple cases just use print. In this case put a print(new_num) after each time you assign to new_num.

In this case you will notice where new_num gets set to the wrong value.

You keep assigning new_num=num*10 instead of multiplying new_num by 10

CodePudding user response:

As I understood from your question, the value of num you are taking as 3. And for total as well the same 3.

Note: Always try to provide some examples while posting your problem so that the answerers could read, understand & suggest, help.

I have a quick solution for you here, just try and try to modify for other purposes.

int(f'{num}' * total) is sufficient to give you that. You can change it a bit and do for floats as well.

>>> num=int(input("what is your number: "))
what is your number: 3
>>> 
>>> total=int(input("how many times do you want this number to appear: "))
how many times do you want this number to appear: 3
>>> 
>>> int(f'{num}' * total)
333
>>> 

Thanks.

  • Related