Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with variable
#1
Hello,
My program :

variable = chocolate
number = 'variable'

print(number... ?)
And I want the answer of the program to be: "chocolate"

How can I do this ?

Thanks !
Reply
#2
you should use the quotes the other way round
variable = 'chocolate'
print(variable)
if you need to pass value from one variable to another
new_variable = variable
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Right now variable "thinks" chocolate is another variable. But you didn't define it anywhere, so it will get confused and you get an error.
If you want to store the string/text "chocolate" to variable you need to put it in quotes (double or single), like you did with variable. Text in quotes is a string, text without quotes is a variable name.

variable = "chocolate"
number = variable
 
print(number)
Reply
#4
My entire code is :

variable = 'chocolate'
variable2 = 'chocolate2'
#variable3 = '.....
And if you want to read a variable :

enter = int(input())  #Answer variable2
Answer : chocolate2

How can I do this ?

Thanks
Reply
#5
Try answer = globals()[input()]
Reply
#6
(Aug-25-2018, 02:55 PM)Gribouillis Wrote: Try answer = globals()[input()]
I don't think it's good idea to show this to a newbie

@O_Pas_Sage: Please, read Why you don't want to dynamically create variables

use some sort of container, e.g.

dict

myvars = {'variable':'chocolate', 'variable2':'chocolate2'}
print(myvars['variable2'])
myvars = {'1':'chocolate', '2':'chocolate2'}
print(myvars['2'])
or list, tuple

myvars = ['chocolate', 'chocolate2']
print(myvars[0])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Now that @Grib has shown globals()(Python's internal dictionary),okay to show but can also mention better ways.
A ordinarily visible dictionary.
λ ptpython
>>> choco_store = dict(variable='chocolate', variable2='chocolate2')
>>> choco_store
{'variable': 'chocolate', 'variable2': 'chocolate2'}

>>> enter = choco_store.get(input('What chocolate: '), 'No chocolate for you')
What chocolate: variable2
>>> enter
'chocolate2'

>>> enter = choco_store.get(input('What chocolate: '), 'No chocolate for you')
What chocolate: Mars
>>> enter
'No chocolate for you'
Similar to @buran post,did something else for a while so did not see the post.
Reply
#8
(Aug-25-2018, 04:05 PM)snippsat Wrote: Similar to @buran post,did something else for a while so did not see the post.
Yeah, it happens often to me too :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dynamic variable name declaration in OOP style project problem jacksfrustration 3 770 Oct-22-2023, 10:05 PM
Last Post: deanhystad
  Reset Variable problem IcodeUser8 3 2,348 Jul-20-2020, 12:20 PM
Last Post: IcodeUser8
  Variable from notepad problem samuelbachorik 2 2,294 Apr-10-2020, 09:04 AM
Last Post: samuelbachorik
  Problem: Get variable JohnnyCoffee 0 1,592 Feb-22-2020, 09:26 PM
Last Post: JohnnyCoffee
  Problem defining a variable rix 6 3,185 Dec-17-2019, 11:34 AM
Last Post: rix
  Problem writing a variable value to Excel Chuck_Norwich 1 1,929 Jul-25-2019, 02:20 PM
Last Post: Chuck_Norwich
  Pass variable script return twice output problem Faruk 8 4,382 Dec-26-2018, 11:57 AM
Last Post: Faruk
  Trying to create a python exe for an addition problem with an odd variable Python_Newb 0 2,149 Nov-30-2017, 12:18 PM
Last Post: Python_Newb
  Problem With Scope Of A Variable Saif133 5 5,427 Jan-05-2017, 05:18 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020