Name Error: name 'add' is not defined - python

it my first time using Stack Overflow so please excuse any mistakes i have made. Im creating a program and i want ask mathematical questions for the user. But my program will generate two random numbers, and a random arithmetic operator will occur. They are plus, minus and times. So i put them into an array and this is the code.
Code:
The Error i gained:
It said the Error is in Line 10.
Ive tried doing this 'What is'+str(Ran) +,+str(op) +,+str(dom) +'?')
However i gained an invalid syntax on the comma.
Ive tried searching for this particular program, but all of them seem to have something called classes and def in. If it is possible, can i please not use the def and class in my program because i am new to python and i still need to learn what they are.
I am using Python 3.4.2, on a Windows 8 operating system if you wanted to know.
Thanks for reading, i am looking forward to have any assistance in my problem.

+ + is invalid syntax. You should be doing str(something) + ' ' + string(something_else) if you want to add two strings with a space in between. You also need quotes around add, minus and times in the list in order to make them strings.

Related

Newbie at Python, i was trying to create a calculator just for fun, and this error showed up

I'm new to coding and I'm using Python. And I was making a simple calculator, just to mess around with the IDLE, and I keep getting this error:
"ValueError: could not convert string to float: 'bread'"
Here is the code I have written.
total = float("bread") + float("coffee") + float("milk")
I was hoping someone could point me to what is wrong here.
You have to input the actual variable, not the string of the variable. If you put anything between "", it automatically becomes a string.
Try this:
total = float(bread) + float(coffee) + float(milk)
It will work.
The reason why your program didn't work is, that you can't calculate the float value of a string. Imagine sitting in a maths class and your teacher tells you to calculate what bread plus coffee is.
Because you put the words between "", the compiler interpreted them as strings and not as your variables.

zybooks for python, whotespace issue

enter image description here
so I keep getting these whitespaces in my output. I have googled but nothing relevant comes back. i have attached my code and my output. can someone tell me what i am doing wrong. I dont have any space in my code, so why is it putting it in my ouput? this is a common problem form me in zybooks and i would really love to know what i am doing wrong so i can get this done and move on to the next assignment without spending 2 hours on a simple exercise.
If you're asking why there are spaces between your outputs of favoriteColor, etc., it's because Python's print function automatically adds spaces between each of its arguments. If you don't want this happening, you can change your code in two ways:
Either add a sep argument to your print, telling Python to add an empty separator between your arguments:
print('\nFirst password:', favoriteColor, chr(95), petName, sep='')
Or, you can append your arguments together yourself, as strings:
print('\nFirst password:', favoriteColor + chr(95) + petName)
Also, I should mention that if you need an underscore, you don't need to call chr(95) -- you can just write the string '_'.

Need Assistance Building an Economic Model

Fairly new to python, know some basics but want to dive straight in and create economic models, play with the math and interpret the ouputs. However, everytime i place a letter in the equation there will be an error. For example;
MoneyDemand = -100+2K
Money Supply = 5000*10K
File "experiment.py", line 20
MD = -100+2K
^
SyntaxError: invalid syntax
This is probably a small issue for most but i need assistance in how to overcome it. Thanks!
You've got three SyntaxErrors here. A SyntaxError means that you've written something that isn't valid python. I'd really suggest you look through some python tutorials to get a grip on how to program python. Even something like learn x in y minutes would probably be helpful.
But in your cases, the fixes are:
Ending a number with k is not valid python. If by that you mean 1000, you should just write 2000 or 10000. (Or, if you want, multiply it by 1000. See how the error points you at the letter k on the line.
You can't have spaces in the names of variables. Money Supply is not a valid name. Python conventions would probably suggest naming it money_supply.
python does not accept K as an indication of 1000.
You can however using scientific notation.
MoneyDemand = -100 + 2e3
MoneySupply = 5000 * 10e3
The e can be either lower or upper case.
Also note that I removed a space from your variable name Money Supply -> MoneySupply otherwise you would have a syntax error on the next line.

Python error in a colon.. cant find an answer

As part of a piece of python work I have to ask for a first name and surname, then repeat it three times if it is correct. When I try it Python puts a syntax error on the colon. What do I need to do to correct it?
Basically whats going on
I've already tried removing the colon, bringing the if line down to one equals, and looking for bracket errors, but I cant find anything.
The colon should be at the end,
if input == yes:
However I cannot see how this can work? Is that the full code? If so, what is the yes variable?
If it was intended to be a string, the line should be,
if input == "yes":
The "" tells Python that it is a string (a word).
A link to Python syntax: https://en.wikipedia.org/wiki/Python_syntax_and_semantics

Python, not understanding double quotes with nothing inside

got a bit of a noob question.
I'm trying to get Metagoofil working because it keeps saying "error downloading webpage" etc etc.
A google search found that I can change a bit of the code in one of the config files and it will work properly again.
I'm having a problem though: this seems to be the code I want to use.
self.url = url.replace("/url?q=", "", 1).split("&amp")[0]
BUT, it doesn't seem to like me (based on syntax highlighting) have those two quotation marks together with nothing in between. When they are like above, it starts highlighting .split(" up to here thinking that this is the string.
My question is, how can I make the double quotations together without anything in the middle and have it register as its own string, so it doesn't highlight the .split("
The literal "" is a valid string with a length of zero. I guess your syntax highlighter is not working properly.

Categories

Resources