How do i code out in python such that when user type 3, system will output 3 ^? For example,
userinput: 3
system: ^^^
userinput: 4
system: ^^^^
am i supposed to use a for loop and loop over it? Im very new to python, please help. thanks!
in its very simplest form:
from_input = raw_input()
print int(from_input) * '^'
This will parse the incoming string to integer.
However, to note here is that if you put anything else than a string that can be parsed to an it will raise a ValueError
.
A safer approach would then be
from_input = raw_input()
try:
int(from_input) * '^'
except ValueError:
print "Can't cast {0} to int".format(from_input)
First,check the input. Then
print('^'*i)
In Python3 (In Python2, you should use raw_input instead of input)
>>> x = input("userinput: ")
userinput: 4
>>> print("{:^^{}}".format("", x))
^^^^
Or as a program
x = input("userinput: ")
print("{:^^{}}".format("", x))