0

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!

4

3 回答 3

3

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)
于 2013-05-06T07:10:35.903 回答
0

First,check the input. Then

print('^'*i)
于 2013-05-06T07:11:16.120 回答
0

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))
于 2013-05-06T07:17:44.287 回答