How to concatenate a string using a for loop n times? - python-3.x

I have a variable = "v". I would like to concatenate to this variable as many v as input by the user. So, if the user enters 10, the output would be vvvvvvvvv. Although I have it working as defined, I'm not sure if this is the right approach because I have to use a for loop to concatenate as many v as required to the original variable created in step 1 and then print the variable from step 1.
This is what I have so far:
Create a variable equal to the string "v";
Ask how many ‘waves’ the user would like printed;
Using a for loop, concatenate as many v as necessary to the variable created in step 1;
After the loop has been completed, print the variable from step 1.
variable = "v"
waves = int(input("How many waves would you like to print?: "))
for i in range(waves):
print(variable, end="")

yes i think your answer is correct. also you can use like this
variable = "v"
waves = int(input("How many waves would you like to print?: "))
print(variable*waves)
try this

You could use a for loop to accumulate more of the variable
variable = "v"
waves = int(input("How many waves would you like to print?: "))
for i in range(waves-1):
variable+="v"
print(variable)
Or you could more directly increase the variable to the appropriate length without looping.
variable = "v"
waves = int(input("How many waves would you like to print?: "))
variable*=waves
print(variable)

Related

Automatically give lambda arguments

Say I don't know in advance how many arguments my lambda function will take. Let's say I code something which asks the user how many arguments I want:
num = input('How many args? ')
num = float(num)
Say, the user inputs 4. How would I then do something like this in a function?
func=lambda x,y,z,t : eval(string)
Automatically -- I must stress. I need to have Python give 4 dummy variables in this case to the lambda function (I happened to denote it x,y,z,t as the dummies) just because I've specified num = 4. The user will specify the function of x,y,z,t with a string such as "x+y+z+t". Is this possible?
Here's a working version, I didn't use a function at all, I just used exec to dynamically define variable names so the eval will work then:
var_names = ["x", "y", "z", "t"]
arg_num = int(input('How many args? '))
for i in range(arg_num):
exec(var_names[i] + "=" + input(var_names[i] + "= "))
print(eval(input("provide func ")))

How do I achieve this following function only using while loop?

I'm currently working on this problem that ask me to generate an arrow pattern using loops function that looks something like this:
"How many columns? 3"
*
*
*
*
*
I know I can do this with for loop(probably more efficient too), but that is not what I aimed for. I wanted to achieve this only using while loop.
I have some ideas:
1. I set up a control variable and an accumulator to control the loop
2. I then write 2 separate loops to generate the upper and lower part of the pattern. I was thinking about inserting the space before the asterisks using method like this:
(accumulator - (accumulator - integer)) * spaces.
#Ask the user how many column and direction of column
#they want to generate
Keep_going = True
Go = 0
while keep_going:
Column_num = int(input("How many columns? "))
if Column_num <= 0:
print("Invalid entry, try again!")
else:
print()
Go = 1
#Upper part
while Keep_going == True and Go == 1:
print("*")
print(""*(Column_num - (Column_num - 1) + "*")
...but I soon realized it wouldn't work because I don't know the user input and thus cannot manually calculate how many spaces to insert before asterisks. Now everything on the internet tells me to use for loop and range function, I could do that, but I think that is not helpful for me to learn python since I couldn't utilize loops very well yet and brute force it with some other method just not going to improve my skills.
I assume this is achievable only using while loop.
#Take your input in MyNumber
MyNumber = 5
i = 1
MyText = '\t*'
while i <=MyNumber:
print(MyText.expandtabs(i-1))
i = i+1
i = i-1
while i >=1:
print(MyText.expandtabs(i-1))
i = i-1
Python - While Loop
Well first you have to understand that a while loop loops until a requirement is met.
And looking at your situation, to determine the number of spaces before the * you should have an ongoing counter, a variable that counts how many spaces are needed before you continue. For example:
###Getting the number of columns###
while True:
number=int(input('Enter number of rows: '))
if number<=0:
print('Invalid')
else:
###Ending the loop###
break
#This will determine the number of spaces before a '*'
counter=0
#Loops until counter equals number
while counter!=number:
print(" "*counter + "*")
#Each time it loops the counter variable increases by 1
counter=counter+1
counter=counter-1
#Getting the second half of the arrow done
while counter!=0:
counter=counter-1
print(" "*counter + "*")
Please reply if this did not help you so that i can give a more detailed response

Taking input as much as the given number in python 3

I have been trying/looking for this problem and decided to ask.
I want a simple program that gets an integer as input first of all.
a = int(input())
Then, the program will take input as much as the given number, a.
You may want to use a for loop to repeatedly get input from the user like so:
a = int(input("How many numbers do you want to enter? "))
numbers = list() #Store all the numbers (just in case you want to use them later)
for i in range(a):
temp_num = int(input("Enter number " + str(i) + ": "))
numbers.append(temp_num)

Initialize several variable from console input

as the title say, i need to affect values to several variable from one console input. I would like to store 3 number at once from an input line looking like this: -number1-space-number2-space-number3-
Right now i am doing it like this:
numbers = input("Enter three numbers separated by spaces: ")
nb1 = int(numbers.split()[0])
nb2 = int(numbers.split()[1])
nb3 = int(numbers.split()[2])
But it wouldnt surprise me if you could do something like this:
nb1, nb2, nb3 = input("Enter three numbers separeted by spaces: ",? ,?)
Replace question mark by code actually working.
So if you know a better way of doing this, i would be thankfull.
msg = "Enter three numbers separated by spaces: "
n1, n2, n3 = (int(n) for n in input(msg).split())

using a looop to add user input to the set and dict in order discovered

So I'm trying to write a small program that does a few things. First is:
Write a while loop that repeatedly creates a list of words from a line of input from the user. So I did this:
s = input("Please enter a sentence: ")
while True:
pos = 0
for c in s:
if c == " ":
print(s[:pos])
s = s [pos+1:]
break
pos += 1
else:
print(s)
break
I need to add the user inputted words to the set and dict and then display their value in the order in which the program discovered them. I believe I need another loop but I'm not sure. I'm pretty lost at this point and the above is as far as I can seem to come on this program. Any help is appreciated as I am (obviously)new at python.

Resources