connecting and separating string inputs [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
need help with taking inputs in a a loop like so.
example = input("enter the example !")
Then I need to add that input to one single variable and later print it all out
on separate lines EG:
loop cycle 1:
enter the example ! test1
loop cycle 2:
enter the example ! test2
loop cycle 3:
enter the example ! test3
inputs:
1. test1
2. test2
3. test3
One thing is that I am unable to use .append due to using lists in my case is
not in max efficiency. (probably will need to be taught to use \n)

you can append new line character to input function
for python2.
example = ""
for i in range(1,4):
example = example + str(i)+". " +raw_input("enter the example !") +"\n"
print example
for python3
example = ""
for i in range(1,4):
example = example + str(i)+". " +input("enter the example !") +"\n"
print (example)
output
messi#messi-Hi-Fi-B85S3:~/Desktop/soc$ python sample.py
enter the example !text 1
enter the example !text 2
enter the example !text 2
1. text 1
2. text 2
3. text 2

You can append the additional inputs onto the same string using the += operator
You will however need to declare the variable before using += for the first time
For example:
example = ""
for num in range(1,4):
print "loop cycle {}:".format(num)
example += "{}. {}".format(num, input("enter the example !"))
if num < 3:
example += "\n"
print example
edit: updated to include conditional new lines and input numbers

Related

Python: Help me understand how it can be possible [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
Please help me understand how key variable can have a value a or b?
from collections import defaultdict
dict = defaultdict(list)
group_a_count, group_b_count = map(int, input().split())
for i in range(1, group_a_count + 1):
dict[input()].append(i)
for i in range(1, group_b_count + 1):
key = input()
print(key)
INPUT:
5 2
a
a
b
a
b
a
b
OUTPUT:
a
b
All input values was appended to dictonary in first cycle. And how second cycle understood where to get keys from the dictonary?
It's a bit messy code. The only thing the second loop does, is asking group_b_count times an input, which it then prints and continues with the next iteration... So the output is a and b because of this piece of code:
key = input()
print(key)
which just prints what you gave as input
What's the purpose of this program actually?
And if i use for cycle with this parameters:
for i in range(1, 5):
key = input()
print(key)
in STDOUT will be still a and b. A full purpose of program: "In this challenge, you will be given 2 integers, n and m. There are n words, which might repeat, in word group A. There are m words belonging to word group B. For each m words, check whether the word has appeared in group A or not. Print the indices of each occurrence of m in group A. If it does not appear, print -1."
I misunderstood task from HackerRank and I expected another output.

Printing a character each second [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am fairly new in python and I would like to print a character each second. What I normally see in StackOverFlow are steps to print the characters inside a list per second. What I want to do is basically generate like "*" each second. Let me illustrate.
time = int(input("enter time:...") #user will input any integer as reference for the total time.
# * printed after 1 second
# * //another one printed after 2 seconds
# ...
Thank you!
well, I'm guessing you want to get a total time from the user and print a character with a time interval of 1 second that adds up to the total time?
if that's the case here's the code,
import time #this module helps us use the cpu's time in our python programe
time_limt = int(input("enter your total time: ")) #make sure you dont name this variable as time as it would mess up the refrences
char = str(input("enter the chracter you want to print: "))
for i in range(time_limit): #this is for printing up to the time limit
time.sleep(1) #this stops the programe for 1 second
print(char)```
Maybe, I am not sure your question. I guess you want to ask how to print a charactor by limiting time, every other one second. For example:
import time
print(*)
time.sleep(1)
if you have any question, please tell me, maybe I can help you. Thanks for your reading.
def wait_k_seconds():
import time
k = int(input("enter time: "))
for i in range(k-1):
time.sleep(1)
print('*' * (i + 1), end = '\r')
time.sleep(1)
print('*' * k)

rectangle function using a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I've just started python and am stuck on this assignment where i need to create a function that does the following:
rectangle("ab", 3)
#output:
aba
bab
aba
where 3 is the length and height of the 'rectangle' and "ab" is the string used to draw the rectangle. It should be able to work with any string.
another example would be this :
rectangle("aybabtu", 5)
#output:
aybab
tuayb
abtua
ybabt
uayba
I'd would like to see the model answer to better understand the steps needed to create functions like this in the future, but that is only possible after completing the assignment. Thanks in advance!
Edit: Sorry for not posting my own code, i was kind of self concious and frustrated and scrapped it all, and its my first time asking a question. I will not repeat this mistake again
First I would create long string with repeated text and later I would split it to lines.
I need to calculate how many times repeate text in rectangle - and it has to be rounded up, not rounded down. Then I can use it to create long string - it can be even longer because later I will slice it and it will skip last chars.
After creating long string I can use for-loop to slice it and print only single line.
You may also add line to list and later join lines with '\n' to return string instead of display it.
import math
def rectangle(txt, size):
rect_len = size * size
print('> rect_len:', rect_len)
txt_len = len(txt)
print('> txt_len :', txt_len)
# round up to next int
repeate = math.ceil(rect_len/txt_len)
#repeate = int(rect_len/txt_len) + 1
print('> repeate :', repeate)
long_txt = txt*repeate
print('> long_txt:', long_txt)
for x in range(size):
print(long_txt[:size])
long_txt = long_txt[size:]
# --- main ---
print('---')
rectangle("ab", 3)
print('---')
rectangle("aybabtu", 5)
Result:
> rect_len: 9
> txt_len : 2
> repeate : 5
> long_txt: ababababab
aba
bab
aba
---
> rect_len: 25
> txt_len : 7
> repeate : 4
> long_txt: aybabtuaybabtuaybabtuaybabtu
aybab
tuayb
abtua
ybabt
uayba

How do I add input to a if funtion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How exactly do I add an additional input to this if function, or atleast add a function which will allow me to give the user another option. Thank you, in advance.
ask = input ("Would you like to 1 input an existing number plate \n or 2 view a random number? 1 or 2: ")
if ask == "1":
print("========================================================================")
ask = input()("Please enter it in such form (XX00XXX): " )
If this ask = input()("Please enter it in such form (XX00XXX): " ) is actually in your code, just changing it to ask = input("Please enter it in such form (XX00XXX): " ) will fix your problem. You had one extra pair of brackets.
DISCLAIMER: This works in Python 3.x
For Python 2, use raw_input("Your question") instead of input("Your question").
As an answer to the question in your own answer:
What is wrong in this code?
ask = input ("-Would you like to 1 input an existing number plate\n--or 2 view a random number\n1 or 2: ")
if int(ask) == 1:
print("========================================================================")
while True:
ask2 = input("Please enter it in such form (XX00XXX): " ).lower()
if re.ask3("[a-z]{2}[0-9]{2}[a-z]{3}",ask2):
print("Verification Success")
break
else:
print("Verification Failed")
You have to indent the while loop once (default is 4 spaces) and everything inside of the while loop twice (default 8 spaces). When pasting a code like this, make sure to include all code needed to run. In this case, show your import re (I had to find out myself you imported that).
I hope I helped.
You are comparing an int(1) to str("1").
It should be
if int(ask) == 1:
# rest of your code
or you can use raw_input() function which returns a string value

How would I create a Python program that reads a list of numbers, and counts how many of them are positive [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to create a Python program that will count how many positive numbers there are in a list of numbers. The list of numbers has to be typed in by someone. The end result must be the number of elements in the list that were > 0
For an example, this is what you would see on the screen:
>>>Please enter a list of numbers separated by commas: 1,2,-3,-4,5,-6
>>>3
The answer would be 3 in this example. I am sorry if the question seems stupid, but I am a beginner and I am trying my best.
raw_input() for Python 2.x (input() for Python 3) then split() the string at , and then count positive numebers, Example -
s = raw_input("Please enter a list of numbers separated by commas:")
print(len([i for i in s.strip().split(',') if int(i) >= 0]))
You can try like this. input returns tuple
>>> vals = input('get: ')
get: 1,2,-3,-4,5,-6
>>> len([item for item in vals if item > 0])
3
Python 3, input returns string
>>> vals = input('get: ')
get: 1,2,-3,-4,5,-6
>>> len([item for item in vals.split(',') if int(item) > 0])
3
By the way, zero is neither positive nor negative.

Categories