Why is my function not prompting me to enter input? - python

I’m using Python IDE 3. My goal is this: If I have a string of text, ‘ABCDEFGHIJKL’, I want to sort it into groups, like three groups (‘ADGJ’,’BEHK’,’CFIL’). I require input for this, but the prompts aren’t showing up and I can’t type in input. Here’s my code:
#data
code_text = input('Text: ').lower()
code_skip = int(input('Shift length: '))
code_list = []
#function
def countSkip(text, shift, listt):
i = 0
group = 1
if group <= shift:
for e in text:
#make sure the set starts at the right place
if e.index()+1 < group:
pass
elif shift != 0:
if i = shift:
listt.append(e)
i = 0
i += 1
else:
listt.append(e)
group += 1
Calling the function
countSkip(code_text, code_shift, code_list)

There's a few things stopping your code from working that people have pointed out in the comments. Instead of trying to dissect your code and get that to work, I wrote a much more concise function that will get you the results you're after
def text_splitter(input_text, set_length):
num_sets = int(len(input_text)/set_length)
split_text = ["".join([input_text[(n * num_sets) + m] for n in range(set_length)]) for m in range(num_sets)]
return split_text
text_to_split = input('Text: ').lower()
len_set = int(input('Set Length: '))
text_list = text_splitter(text_to_split, len_set)
Sorry I was struggling to name the variables in an effective manner but the function above uses a list expression to get you the results you need. Keep in mind that if you use say a 7 letter string and ask for sets of length 2, the last letter won't be appended. However this shouldn't be too hard to check and correct. For example you could add this code to the function or around the initial input for the set length:
while len(input_text) % set_length != 0:
set_length = int(input("The text is length " + str(len(input_text)) + " please enter a different set length: "))

Related

No iteration in nested while statement

How the loop should iterate. I'm a beginner trying to create a Python program to print a word backwards based on old knowledge from a few years ago. It does a few other things but they are just printed statements are don't pose a problem. Here is my code:
count = 0
while count < 100:
word_var = input("Enter ONE word with NO spaces:")
def split(word_var):
return list(word_var)
word_array = split(word_var)
m = 0
i = len(word_array)-1-m
print("The number of letters in your word is:", i)
while m < len(word_array):
if m < i:
word_array[m], word_array[i - m] = word_array[i - m], word_array[m]
m = m + 1
else:
break
m = m + 1
print(''.join(word_array))
count = count + 1
print("You've typed:",count,"word(s).")
Here is the problem section:
if m < i:
word_array[m], word_array[i - m] = word_array[i - m], word_array[m]
m = m + 1
else:
break
m = m + 1
My main problem is that it seems like the second while loop is not iterating when the word is more than five letters long. For example, if I input the word "should" into the program I get back out dhouls. It seems as if only one interchange of letters is being performed. I figure this is a problem with the if statement in that nested while loop, but I can't seem to find what is wrong with it. I carefully sketched out how I think the if statement works in the photo attached.
Your if condition is wrong. You want to compare the two indices that you will use in the list, but the second one is not i, but i-m. So change it to:
if m < i - m:
This corrects your issue. It should be noted that in Python you can reverse string just like this:
print(word_var[::-1])
There are two issues:
The counting of the letters isn't correct. You should just output the length of word_array.
You're iterating the while loop too many times. You should terminate it when m equals or exceeds len(word_array) // 2. Otherwise, you'll unreverse the letters and get the original word back.
i = len(word_array)-1
print("The number of letters in your word is:", len(word_array))
while m < len(word_array) // 2:
word_array[m], word_array[i - m] = word_array[i - m], word_array[m]
m = m + 1
This outputs:
Enter ONE word with NO spaces:should
The number of letters in your word is: 6
dluohs
You've typed: 1 word(s).
I like your project and appreciate your efforts.
This a another way to reverse a string using a list variable and the insert() method.
word_array = []
word_var = input('Your word : ')
word_array = []
for c in word_var:
word_array.insert(0, c)
word_reversed = ''.join(word_array)
print(word_var, '->', word_reversed)
output :
should -> dluohs

basic encryption in python using loop and if elif

I am trying to make a caesar cipher encryption program in python using loop and if/elif, but I my program returns unwanted results. There may be a little error in my programming logic and maybe someone wants to help me to fix it.
this input :
caesar
2
and should show an output:
ecguct
but my program show output : caesar #thats wrong
This my code I'm trying:
x = []
ch = ""
i = 0
x = input(" enter words : ")
y = int(input("enter number : "))
for i in range(len(x)):
i+1
ch = x[i]
if(ord(ch)>=ord('a') and ord(ch)<=ord('z')):
chr(ord(ch)+y)
elif(ord(ch)>ord('z')):
(ord(ch)- ord('z')-ord('a')-1)
x[i]=ch
elif(ord(ch)>=ord('A') and ch<=ord('Z')):
chr(ord(ch)+y)
elif(ord(ch)>ord('Z')):
(ord(ch)- ord('Z')+ord('A')-1)
x[i]=ch
print(x)
I feel unsure about iteration that I made i+1 , and also x[i]=ch it's right syntax?. Also I use ord() to change value string to integer. I need your opinion to fix it.
You got several bugs in your code. First of all you have to turn your calculations to char again using the chr() function. Sometimes you do, sometimes you don't. Then you can't index strings - here x[i]=ch. Instead you have to assign your result to a new string using += operator or some other append method. At last your if and elif does not cover the overflows it should. In case you input a string with a letter y, z, Y or Z it will overflow. The if-questions covering those overflows need to be nested inside the top level if-elif which processes the different upper case and lower case letters.
There is also a minor bug within the overflow calculation where you use a - minus instead of a + plus operation.
Here is the slightly fixed version of your code:
x = input(" enter words : ")
y = int(input("enter number : "))
y = y % 26 # limit y to the amount of possible letters
result = ""
for ch in x:
if(ch>='a' and ch<='z'):
ch = chr(ord(ch)+y)
if(ch>'z'):
ch = chr(ord(ch)- ord('z')+ord('a')-1)
elif(ch>='A' and ch<='Z'):
ch = chr(ord(ch)+y)
if(ch>'Z'):
ch = chr(ord(ch)- ord('Z')+ord('A')-1)
result += ch
print("input:", x)
print("output:", result)
You also can iterate directly over the letters of a string like 'for ch in x' does without the need of extra indexing with x[i]. The ord(...) function is not required for comparison of characters.
Some further shrinking:
x = input(" enter words : ")
y = int(input("enter number : "))
result = "".join ( [ [ # encrypting a string by adding a number to all letters
c, chr((ord(c)-ord('Aa'[c.islower()])+y)%26+ord('Aa'[c.islower()]))
] [ c.isalpha() ] for c in x ] )
print("output:", result)
This code is harder to read and should cover some remarks on what it actually does. If you have some more code, shrinking might make it easier to understand because otherwise you have tons of files and modules within a project.

How to convert user input into a list and remove all occurrences of a particular digit or character?

I assume I am tackling this problem from the wrong angles or lack the proper understanding of built-in functions and syntax to resolve my issues despite my trying. I also discovered using:
input("Enter input: ")
is dangerous and should use raw_input instead, but can't seem to get this functioning correctly.
I want to write a script that can take user input, convert it into a list, multiply each individual element by 3, scan the solution to that problem for any "3" and remove all occurences of "3" while maintaining all other figures in their respective elements in the list, to then print or return the final result.
ex. entering 1 2 3 would output 6 9 as 1 * 3 = 3 which would be omitted.
ex. entering 2 6 11 would output 6 18 as 11 * 3 = 33 and both 3's would be omitted.
If for instance, user was to input x where x * 3 = 2381193 - I want for this to be converted to 28119.
I believe the simplest way to do this would be to convert to a string?
I started with something like this:
userInput = input("list element seperated by space: ")
nums = userInput.split()
x = int(nums[0]) * 3
y = int(nums[1]) * 3
z = int(nums[2]) * 3
output = x, y, z
negate = list(output)
if "3" in str(x):
del negate[0]
if "3" in str(y):
del negate[1]
if "3" in str(z):
del negate[2]
print(negate)
and now I've re-thought it to:
userInput = raw_input("list element seperated by space: ")
nums = userInput.split()
for x in list(nums):
numsList = int(x) * 3
output = list(numsList)
y = "3"
while y in str(output): output.remove(y)
print(output)
But overall, I am unable to achieve the desired result.
Could you please provide feedback?
It would be greatly appreciated :)
In your case, your first snippet was close to making a solution to your problem. The only thing you would want to do, is to change the conditional statement into an interative statement, like so:
nums = str(input("list element seperated by space: ")).split(" ")
num_list = []
if len(nums) != 1:
num_list = [*(int(num.replace("3", "")) * 3 for num in nums)]
else:
num_list = [nums[0]]
for x in num_list:
if x != 3:
print(str(x).replace("3", "") + " ", end="")
else:
print("0 ", end="")
If you input 11 you would receive 0 and if for instance you had 133 - you would want to remove the 3 but keep the 1.
This part of your question is not clear/elaborated enough hence i did not include this logic in my snippet above. So anyone is free to edit my answer and include it once the point above is clear.
[EDIT]: So i made changes to my snippet above. I have not tested this so there is a chance for it to be wrong. If that is the case, then inform me of any specific patterns/tests that would end as unwanted.

How to let user end program and shift list

Im trying to make an encryption program
def intro():
msg = input("Enter the message you wish to encrypt: ")
return msg
def shift(msg):
alpha = ['a', 'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
rotate = int(input("The level of the encryption?: "))
text = ""
for ch in msg:
if ch == " " or ch == ".":
pass
elif msg == "$":
print("nah")
else:
index = alpha.index(ch)
newindex = index + rotate
new= alpha[newindex]
text += new
return text
def start():
msg = intro()
text = shift(msg)
print("Your encryptions is: " + text)
start()
I can't figure out a way to loop the list without getting an index out of range error. For example, if you put "z" it will shift to an "a". I also need for my program to loop till user inputs to end it. I just started coding in python a few months ago so any help will be appreciated!beginner
All you need to do is add this line
newindex %= len(alpha)
Detailed Change (with context)
index = alpha.index(ch)
newindex = index + rotate
new= alpha[newindex]
text += new
to
index = alpha.index(ch)
newindex = index + rotate
newindex %= len(alpha) # <--- this is the new line
new= alpha[newindex]
text += new
This will automatically make the new index loop so it never goes past the end!
Working example
>> Enter the message you wish to encrypt: 'xyz'
>> The level of the encryption?: 2
>> Your encryptions is: zab
Since your code is running fine, I can tell you about some techniques you can work on to get the functionality you want.
To get an array that loops around, you can use a mod system. For example 8 mod 3 = 2 and it would be coded remainder = 8 % 3. If you had a mod size 26, i.e. the alphabet, you could take the remainder of the total number and use it as an index in your alphabet list. This would cycle around when the total number is greater than 26 and begin again at a.
To get the program to end on user input, you can use a variety of methods such as keyboard interrupts, recognizing certain commands such as ctrl-c or whole words. Here is a start from a previous stackoverflow question. How to kill a while loop with a keystroke?
use the modulus operator to wrap the index around when it's outside the list length:
newindex = (index + rotate) % len(alpha)
To repeat, use a while True: loop, and use break to end it.
def start():
while True:
msg = intro()
if msg == '':
break
text = shift(msg)
print("Your encryptions is: " + text)
This will end when the user inputs an empty line.

Process multiple input values in Python

Hi so I'm very very new to programming so please forgive me if I'm not able to ask with the correct jargon, but I'm writing a simple program for an assignment in which I'm to convert CGS units to SI units
For example:
if num == "1": #Gauss --> Tesla
A=float(raw_input ("Enter value: "))
print "=", A/(1e4), "T"
with this I'm only able to convert one value at a time. Is there a way I can input multiple values, perhaps separated by commas and perform the calculation on all of them simultaneously and spit out another list with the converted values?
You can read in a comma-separated list of numbers from the user (with added whitespace possibly), then split it, strip the excessive white space, and loop over the resulting list, converting each value, putting it in a new list, and then finally outputting that list:
raw = raw_input("Enter values: ")
inputs = raw.split(",")
results = []
for i in inputs:
num = float(i.strip())
converted = num / 1e4
results.append(converted)
outputs = []
for i in results:
outputs.append(str(i)) # convert to string
print "RESULT: " + ", ".join(outputs)
Later, when you're more fluent in Python, you could make it nicer and more compact:
inputs = [float(x.strip()) for x in raw_input("Enter values: ").split(",")]
results = [x / 1e4 for x in inputs]
print "RESULT: " + ", ".join(str(x) for x in results)
or even go as far as (not recommended):
print "RESULT: " + ", ".join(str(float(x.strip()) / 1e4) for x in raw_input("Enter values: ").split(","))
If you want to keep doing that until the user enters nothing, wrap everything like this:
while True:
raw = raw_input("Enter values: ")
if not raw: # user input was empty
break
... # rest of the code
Sure! You'll have to provide some sort of "marker" for when you're done, though. How about this:
if num == '1':
lst_of_nums = []
while True: # infinite loops are good for "do this until I say not to" things
new_num = raw_input("Enter value: ")
if not new_num.isdigit():
break
# if the input is anything other than a number, break out of the loop
# this allows for things like the empty string as well as "END" etc
else:
lst_of_nums.append(float(new_num))
# otherwise, add it to the list.
results = []
for num in lst_of_nums:
results.append(num/1e4)
# this is more tersely communicated as:
# results = [num/1e4 for num in lst_of_nums]
# but list comprehensions may still be beyond you.
If you're trying to enter a bunch of comma-separated values, try:
numbers_in = raw_input("Enter values, separated by commas\n>> ")
results = [float(num)/1e4 for num in numbers_in.split(',')]
Hell if you wanted to list both, construct a dictionary!
numbers_in = raw_input("Enter values, separated by commas\n>> ")
results = {float(num):float(num)/1e4 for num in numbers_in.split(',')}
for CGS,SI in results.items():
print "%.5f = %.5fT" % (CGS, SI)
# replaced in later versions of python with:
# print("{} = {}T".format(CGS,SI))

Categories