python - return and print does not give same result

2024/11/17 6:30:27

what I'm trying to do is take the entered string separated by commas and change it to list then for each list as a key print the associated value.

def main():myDict = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5....}u_input = input("Enter a letter")myList = [x.strip() for x in u_input.split(',')]result = searchDict(myList)print(result)def searchDict(key):for ml in key:value = myDict.get(ml, "not found")re = []re.append(value)print('-'.join(re)) #this one shows each value for each entered key separated by comma but it does not print it in one line also prints 'None' on the end#res = '-'.join(re)#return res //this only shows the value for the first key only even if I enter multiple lettermain();

The prob is that, if I return 'res' instead of printing it, I'm only getting the first key value.

output with print(if I enter a, b): 1, 2, none

output with return 1

Answer

Are these lines indented properly?

for ml in key:
value = myDict.get(ml, "not found"); 
re = [];
re.append(value)

I think you want it to do this:

re = []
for ml in key:value = myDict.get(ml, "not found")    re.append(value)
return '-'.join(re)

BTW no semicolons used in Python.

https://en.xdnf.cn/q/120234.html

Related Q&A

Creating a menuBar in a frame?

import Tkinter as tk from Tkinter import Label, StringVar, Entry, Button, Menu, Frame, Tk, Canvas import subprocess from Tkconstants import LEFT, CENTER,W, SUNKEN , X, Yclass SampleApp(tk.Tk):def __ini…

Python return dictionary [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Use selenium and python to move mouse outside of web page area

I need to test the exit intent form on this page: https://www.graphicproducts.com/guides/5s-system/When the mouse pointer is moved outside of the web page area a popup window appears. I then need to en…

Python „stack overflow” (104 if statements). Is def(x) the only solution to optimise code?

Today have tried to check files with path directory name. Previously it worked, until I tried to create 104 if/else statements. How to dispose of this overflow error? More specific question: Does def(…

How to fix Python restarting whenever I start program on IDLE environment?

import randomwinning_conditon = 0 no_of_guesses = 0 comp_guess = random.randint(1,100)while (no_of_guesses == 11) or (winning_conditon == 1):user_guess = int(input("What is your guess? "))if…

Fetching Lawyers details from a set of urls using bs4 in python

I am an absolute beginner to Web Scraping using Python and know very little about programming in Python. I am just trying to extract the information of the lawyers in the Tennessee location. In the web…

Having trouble with python simple code running in console [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…

.upper not working in python

I currently have this codenum_lines = int(input()) lines = [] tempy = ctr = 1 abc = {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 } for i in range(0, num_lines):tempy = input()lines.append([])l…

Python SKlearn fit method not working

Im working on a project using Python(3.6) and Sklearn.I have done classifications but when I try to apply it for reshaping in order to use it with fit method of sklearn it returns an error. Heres what …

extracting n grams from huge text

For example we have following text:"Spark is a framework for writing fast, distributed programs. Sparksolves similar problems as Hadoop MapReduce does but with a fastin-memory approach and a clean…