0

The code is supposed to print :) based on the number inputted (an integer 1 to 10). With any positive integer, the code is supposed to print that many smiley faces (ex. if 5 is entered, 5 smiley faces should be printed).

It's required that the code should use += to add onto the end of a string and should also decrement to count down and use a loop.

x = input("enter number 1 to 10: ")
for i in len(x):
    print(":) " * x)

I don't think you can multiply int values and str values, and I can't find another way to do this.

CoolCoder
  • 786
  • 7
  • 20
hithit
  • 27
  • 3
  • 1
    You should find it here: https://stackoverflow.com/questions/64473935/how-to-print-a-string-x-times-based-on-user-input – Programming Rage Apr 30 '21 at 04:46
  • 3
    Does this answer your question? [How to print something a specific number of times based on user input?](https://stackoverflow.com/questions/63814825/how-to-print-something-a-specific-number-of-times-based-on-user-input) – Programming Rage Apr 30 '21 at 04:56

4 Answers4

1

First things first, input() function returns a string. You want x to be an integer, use the int() function:

x = int(input("enter number 1 to 10: "))

Second You can either use a for loop or the multiplication operator to print n number of smileys.

# Using for loop to print n smileys :
x = int(input("enter number 1 to 10: "))
for i in range(x):
    print(":) ",end="")
# Using multiplication operator :
x = int(input("enter number 1 to 10: "))
print(":) "*x)
Pervez
  • 391
  • 1
  • 10
0
# x should be an integer
x = int(input("enter number 1 to 10: "))
# define "s" as an empty string
s = ''
# increment ":)" to "s" x times
for _ in range(x):
    s += ':)'
print(s)
Gusti Adli
  • 1,225
  • 4
  • 13
0

You're almost there! In fact, you can multiply string and integer values. This code does what you described:

print(':) '*int(input("Enter number 1 to 10: ")))

If you really want to use a for loop and +=, you could also do this

x, s = input("enter number 1 to 10: "), ''
for i in range(x):
    s += ':) '
print(s)
v0rtex20k
  • 1,041
  • 10
  • 20
0

Answer: How to print a string x times based on user input
Credits to: NoobCoder33333

times = input('Enter number from 1 to 10: ')
word = ":-)"

print('\n'.join([word] * int(times)))
Programming Rage
  • 403
  • 1
  • 4
  • 18