Encryption code in def function to be written in python

2024/10/6 9:14:29

need some help in the following code as it goes into infinite loop and does not validate user input: the get_offset is the function. Just edited need some help with the encryption part to be done in a function defined

def get_offset(offset):while True:value = (offset)if value < 1:print("")if value > 94:print("")return offsetmy_details = display_details()get_choice = get_menu_choice()print(my_details)
print(get_choice)count = 10while count > 0:count -=1encrypted = ""choice = int(input("What would you like to do [1,2,3,4,5]: "))while choice <1 and choice > 5:print("Invalid choice, please enter either 1, 2, 3, 4 or 5.")if choice == 1:string_input = input("Please enter string to encrypt: ")input_offset = get_offset(int(input("Please enter offset value (1 to 94): ")))**for letter in string_input:x = ord(letter)encrypted += chr(x + input_offset)if x < 32:x += 94if x > 126:x -= 94print(encrypted)**
Answer

you have to assign choice new value inside inner while loop. this way, new input is considered before iterating again and again.

this should do the trick:

while choice < 1 and choice > 5:choice = int(input("Invalid choice, please enter either 1, 2, 3, 4 or 5."))

A random advice: If you want to keep dataset strictly equal to [1,2,3,4,5], you should avoid choice<1 and choice>5. This makes your program vulnerable to invalid inputs such as 3.5.

Instead, you should do this:

valid_inputs = [1, 2, 3, 4, 5]choice = int(input("Pick a number from [1, 2, 3, 4, 5]:"))while choice not in valid_inputs:choice = int(input("Invalid choice, please enter either 1, 2, 3, 4 or 5."))<rest of stuff here>

What I understood from your comments is that, in get_offset(), you want user to type a value between 1 and 94. If user enters something out of that range, you want to ask question again and again. The code below should do what you want:

def get_offset():# first, ask for a value.offset = int(input("Enter a value between 1 and 94:"))while offset < 1 and offset > 94:# if the value is invalid, trap user inside this while loop.# user will be stuck here (while loop will return true) # until a valid input is received.offset = int(input("Invalid input. Please enter a value between 1 and 94:"))# if we proceed down here, it means we are over while loop # and it implies that user has given us valid input. return value.return offset

Secondly, inside main while loop, you can call get_offset() like this:

if choice == 1:string_input = input("Please enter string to encrypt: ")input_offset = get_offset()
https://en.xdnf.cn/q/119515.html

Related Q&A

Creating xml from MySQL query with Python and lxml

I am trying to use Python and LXML to create an XML file from a Mysql query result. Here is the format I want.<DATA><ROW><FIELD1>content</FIELD1><FIELD2>content</FIELD2…

How to add another iterator to nested loop in python without additional loop?

I am trying to add a date to my nested loop without creating another loop. End is my list of dates and end(len) is equal to len(year). Alternatively I can add the date to the dataframe (data1) is that …

How to know where the arrow ends in matplotlib quiver

I have programmed plt.quiver(x,y,u,v,color), where there are arrows that start at x,y and the direction is determined by u,v. My question is how can I know exactly where the arrow ends?

how send text(sendkey) to ckeditor in selenium python scripting -chrome driver

I cant send a text to the text box of CKEditor while I scripting.it not shown in the seleniumIDE recording also.Help me to fix this issueASAP

How to replace the column of dataframe based on priority order?

I have a dataframe as follows df["Annotations"] missense_variant&splice_region_variant stop_gained&splice_region_variant splice_acceptor_variant&coding_sequence_variant&intron…

Scraping from web page and reformatting to a calender file

Im trying to scrape this site: http://stats.swehockey.se/ScheduleAndResults/Schedule/3940And Ive gotten as far (thanks to alecxe) as retrieving the date and teams.from scrapy.item import Item, Field fr…

Python Text to Data Frame with Specific Pattern

I am trying to convert a bunch of text files into a data frame using Pandas. Thanks to Stack Overflows amazing community, I almost got the desired output (OP: Python Text File to Data Frame with Specif…

Python Multiprocess OpenCV Webcam Get Request [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

error when trying to run my tensorflow code

This is a follow up question from my latest post: Put input in a tensorflow neural network I precoded a neural network using tensorflow with the MNIST dataset, and with the help of @FinnE was able to c…

ValueError: invalid literal for int() with base 10: Height (mm)

import csv from decimal import *def mean(data_set):return Decimal(sum(data_set)) / len(data_set)def variance(data_set):mean_res = mean(data_set)differences = []squared_res = []for elem in data_set:diff…