Initialize several variable from console input - python-3.x

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())

Related

How to divide numbers from a string into two lists according to what operator(+ or -) preceds them?

string = "6+324-909+5+55-1"
listPositive = []
listNegative = []
I would like to put numbers with "+" in front of them in listPositive and numbers with "-" in front of them in listNegative. Just the numbers without operators, but using the operators for distinction. I have managed to find a way to seperate the first number which has neither plus nor minus in front of it.
I am very very green and I will gladly hear about different ways to go about it or even suggestions for a whole different line of thinking.
I tried:
s1='678-6783742+2342+4-8'
lst=s1.split()
sample=[]
for i in lst:
if '-' in i:
sample.append(i[1:])
print(sample)
And was expecting:
['6783742', '8']
But it only worked that way when I put spaces in like this:
s1='678 -6783742 +2342 +4 -8'
In your question you already have the answer: replace- with - and + with + (note the space before operator) and then split:
s = "678-6783742+2342+4-8"
s = s.replace("+", " +").replace("-", " -")
negative = []
for i in s.split():
if i.startswith("-"):
negative.append(i[1:])
print(negative)
Prints:
['6783742', '8']

Problem when printing output in Python (with easyinput)

So I'm having trouble with enters and line breaks in my code. I must use easyinput library (and import read).
My code stands for:
Input: Input consists of several cases separated by an empty line. Every case has three parts ('lines'). The first one is a line with the translation table: 26 different characters (with no spaces nor ‘_’), the first one corresponding to ‘a’, the second one to ‘b’, …, and the last one to ‘z’. The second part is a number n > 0 in a line. The third part consists of n encrypted lines of text.
Output: For each case, write the original text, also with n lines. Change each ‘_’ of the encrypted text for a space. Write an empty line at the end of each case.
So I figured out how to solve the problem, the things is that my code prints 'well' entering line by line in input. But the problem input must be entered the whole entire. I put an example for better understanding:
Some input should be:
52-!813467/09*+.[();?`]<:>
6
5_3++!_305))_6*_;48_26)4+.)_4+);80_6*_;48_!8`60)_)85;
;]8*;:_+*8_!83(88)_5*!_;46(;88*_96*?;8)
*+(;485);_5*!_2:_*+(;4
956*_2(5*-4_)8`8*;4_0692_85);_)6!8
)4++;_1(+9_;48_081;_8:8_+1_;48_!85;4)_485!
5_288_06*8_1(+9_;48_;(88_;4(+?34_;48_)4+;_161;:_188;_+?;
bcdefghijklmnopqrstuvwxyza
3
cfxbsf_pg_cvht_jo_uif_bcpwf_dpef
j_ibwf_pomz_qspwfe_ju_dpssfdu
opu_usjfe_ju
And its output must be:
a good glass in the bishops hostel in the devils seat
twenty one degrees and thirteen minutes
northeast and by north
main branch seventh limb east side
shoot from the left eye of the deaths head
a bee line from the tree through the shot fifty feet out
beware of bugs in the above code
i have only proved it correct
not tried it
My code far now is:
from easyinput import read
abc = 'abcdefghijklmnopqrstuvwxyz'
values = [letter for letter in abc]
old_abc = read(str)
while old_abc is not None:
keys = [old_letter for old_letter in old_abc]
dict_abc = dict(zip(keys, values))
num_lines = read(int)
for i in range(num_lines):
line = read(str)
for j in line:
if j == '_':
print(' ', end = '')
else:
print(dict_abc[str(j)], end = '')
print('\n')
old_abc = read(str)
I do not find a way of making my code easier, I just want some help to finally print the desired output. Thanks

How to concatenate a string using a for loop n times?

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)

How to solve an a+b equation?

New to python, what am i doing wrong here that it wont print my total out?
second = 2
third = 3
extra = input("what is the extra value?")
total = ("first+second+thrid+extra")
print("total,")
You are printing a string by putting in double quotes
Whereas expression should be like this:
result = first + second + extra
print(result) # without quotes

Delete repeated characters in strings in cell array

I have a cell array like this :
Input = {'CEEEGH';'CCEEG';'ABCDEFF';'BCFGG';'BCDEEG';'BEFFH';'AACEGH'}
How can I delete all of the repeated characters and just keep only 1 character left in each string in the Input ? The expected output should be like this:
Output = {'CEGH';'CEG';'ABCDEF';'BCFG';'BCDEG';'BEFH';'ACEGH'}
use :
cellfun(#unique,input,'UniformOutput',0)
ans =
'CEGH'
'CEG'
'ABCDEF'
'BCFG'
'BCDEG'
'BEFH'
'ACEGH'
EDIT:
To conserve ordering in case the letters are not sorted, as #thewaywewalk commented, you can use:
cellfun(#(x) unique(x,'stable'),input,'UniformOutput',0)

Resources