use variables in dict python 3 [closed] - python

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 reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want, use variables in dict, how can I?
ex:
field_one= input("Please Enter Field Name? ==> ")
field_two= input("Please Enter Field Name? ==> ")
fields_data = dict(field_one=data_one, field_two=data_two)
print (fields_data)
my problem is, the output not show user input, just show:
'field_one' = 'data_one'
'field_two' = 'data_two'

Use a dict literal:
fields_data = {field_one: data_one, field_two: data_two}

Related

Why isn't the except NameError picking up the name error on my code? [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 reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm trying to have a user input an integer, while trying to catch errors if they put in floaters or strings. Any help would be awesome guys!!
try:
user_input = int(input())
except ValueError:
print("Please enter a whole number with no decimal points")
except NameError:
print("user_input not defined")
NameError is called when you try to access a variable not declared yet. But in this code, you will always create user_input, so the try block can't go to the nameerror block.

How to extract integers from a random string without using regex? [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 reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Say I have an arbitrary string like
"abc 123 def 456"
How would I take out the integers so that it can print "123456" without using regex?
for your specific question, you can just use .isdigit()
s = "abc 123 def 456"
for ch in s:
if ch.isdigit():
print(ch, end='')
print()

Read 2d array and list result in Python [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 reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have following 2D array
name_list = [['Orange', '5'],['Mango','6'],['Banana','3']]
I want to get each fruit name alone with its count and print it using a python code. So how do I read above array to extract the data (inside for loop)
I need print out as
Name:Orange<br/>
Count:5<br/>
Name:Mango<br/>
Count:6<br/>
Name:Banana<br/>
Count:3<br/>
You can unpack your list like this:
for name, amount in name_list:
print("Name:{}".format(name))
print("Count:{}".format(amount))
Try this:
name_list = [['Orange', '5'],['Mango','6'],['Banana','3']]
for item in name_list:
print("Name: {}".format(item[0]))
print("Count: {}".format(item[1]))

add keys and values from input method [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 reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a task and I did almost of it, but I'm stuck in how i will add keys and values into dictionary from user I think that I have to use input method but I'm not sure .
This code is what i done it shows the max value and key but i add the dictionary i want something like this
x= {'omar':20,'nagy':5}
maxKey= max(x, key=x.get)
maxValue=max(x.values())
print maxKey,maxValue
but the user is the one who enter the key and value
you can do:
count=2
x= {}
while count:
name=input('name:')
value=int(input('value:'))
x[name]=value
count-=1
maxKey= max(x, key=x.get)
maxValue=max(x.values())
print maxKey,maxValue
which will result in:

String conversion error when reading user input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I get the following error
ValueError: could not convert string to float: 'asdf\n'
from this code:
import sys
print('Hello, this is a short quiz. Please tell me your name')
name = int(sys.stdin.readline())
print('Are you ready %s?' % (name))
Unless your name is "7", that code is guaranteed to fail. You are casting the input string to an int. Try:
name = sys.stdin.readline().strip()

Categories

Resources