-1
print("input a: ")

a = input()
b = a * 20

print(str(a) + " = " + str(b))`

Hi guys, I'm newbie with python. i'm give a = 1, the result is "1 = 11111111111111111111". Same a = 2, the result is "2 = 22222222222222222222". I don't know the reason. help me

Janez Kuhar
  • 3,096
  • 2
  • 22
  • 39
Hung
  • 1
  • `print("input a: ") a = int(input()) b = a * 20 print(f"{int(a)} = {int(b)}")` – Riceblades Jun 01 '22 at 21:21
  • This is because the `input()` function returns a `str`ing by default, not an `int`eger. Therefore, you are multiplying the **string** `"1"` by 20 to get an answer of `"11111111111111111111"`, whereas you want to multiply the integer `1` by 20 to get `20` – Lecdi Jun 01 '22 at 21:21
  • Another check to do is to give 'Q' as the input to `a`, or 'dog' and see what output you get – G. Anderson Jun 01 '22 at 21:21
  • Welcome to Stack Overflow. Please read [ask], especially the guidance on titles. Note carefully that this is **not a discussion forum**. – Karl Knechtel Jun 01 '22 at 21:21
  • the input function give you a string, if you want the numeric value you need to transform it into one, additionally strings have the feature that if you multiply it by a number you get a new string that is the original concatenate with itself that many times – Copperfield Jun 01 '22 at 21:24
  • i give a = fa , the result is "fa = fafafafafafafafafafafafafafafafafafafafa" – Hung Jun 01 '22 at 21:29
  • i corrected like you, Riceblades. – Hung Jun 01 '22 at 21:32
  • it still error, – Hung Jun 01 '22 at 21:32
  • ValueError: invalid literal for int() with base 10: 'a' – Hung Jun 01 '22 at 21:32

0 Answers0