I am still pretty new to Python and learning! I searched around and some postings seem too complex for me at this time. Wondering why the car_brandp below is not joining with "and quite expensive" after the else function initiates? The first else line prints fine but it seems like I can't put that message as a variable?
I got the None Type error
car_brand =input ("What is the best car brand? ")
if car_brand == ("Range Rover"):
print (car_brand + " is the best car brand ever!")
else:
car_brandp = print (car_brand + " is just personal taste..")
print (car_brandp + " and quite expensive...")
This line:
car_brandp = print (car_brand + " is just personal taste..")
is suppose to be:
car_brandp = (car_brand + " is just personal taste..")
"print" is a procedure to display something in the console. A procedure differs from a function as it is not meant to return something of value, but rather perform something as a side effect (it will do something useful but you cannot interact with it). You may not assign the return value of the print function as it is meaningless.
Since you are still new to Python, it is a good idea to learn the proper habits early. In particular, PEP8 contains valuable information on style and conventions that most Python developers follow. Such recommendations are optional, but when followed, they help other developers understand your code better.
car_brand = input("What is the best car brand? ")
if car_brand == "Range Rover":
msg = car_brand + " is the best car brand ever!"
else:
msg = car_brand + " is just personal taste.."
msg += " and quite expensive..."
print(msg)
print() is like a void function in other languages. So, it does something, but it returns nothing (None, in python, like null in other languages).
So, in your next line, you are trying to add
None + "personal taste"
and you get an error because addition of string and None is not defined
So, you options are
consecutive prints
concatenate strings eg print( str(brand) + "personal taste")
formatted strings eg print( f'{brand} personal taste')
Related
I'm trying to get my code to get a string of data from a sensor, and then do something when it reads a specific value.
The following code is how I'm receiving the data now (this is just a test) the function is called earlier in the code at the time where I want it to be called.
def gettemperature(self)
temp = self.board.temp_sensor
print("Temperature is " + str(round(temp)) + " degrees.")
This code works, and returns the temperature value rounded to the terminal, but how could I, instead of printing the value to the terminal, make it so when that string of rounded value is say, 200 degrees, then it prints the temperature value? instead of printing it every 2 seconds (the frequency of the data being received, as per another part of the code)
Using something like
if temp = 200
then print(blahblah)
in short, the above code is what I'm trying to do. If the temp equals a certain value, then something else will happen.
That last code doesn't work. I'm pretty new to coding, so I'm assuming I'm either not going about this the right way, or the syntax of how I'm going about trying to get that value to do something isn't correct (obviously)
Thanks for any help! I'm surprised I got this far, haha.
It would be better if your function gettemperature would return something and then print the result in the condition:
def gettemperature()
temp = board.temp_sensor
return temp
temp = gettemperature()
if temp == 200:
print("Temperature is " + str(round(temp)) + " degrees.")
Before using stackoverflow, I'd recommend learning all this stuff from some basic course, as you'll get to learn the stuff yourself rather then get the answer from someone else.
Try learning conditional statements.
what you want is, to put a conditional statement which triggers if temperature is greater than 200.
If the temp is always a number in string data type, you can use the below code.
def gettemperature(self):
temp = self.board.temp_sensor
print("Temperature is " + str(round(temp)) + " degrees.")
temp=int(temp) #typecasting string datatype to integer
if temp == 200:
print("Temperature is high")
Hi im a beginner python user. I coded a thing just for practice but im getting ''none'' instead of the result. code is like this :
code
i think im using str function wrong but i dont know how to fix it.
thanks for your help
See, when you don't use the return keyword, a function returns None. So when you print a function which returns None, Then None will be printing.
So, use the return keyword.
def work_time(mathematic, chemistry, biology):
print(mathematic*2 + chemistry*1 + biology*(0.5))
return mathematic*2 + chemistry*1 + biology*(0.5)
print("I will study " + str(work_time(1, 2,3) ) + " hours today")
I can't see an image or code
but things that can result to none can be:-
1.Not returning a blue from function
2.Keeping a boolean inactive
3.Not defining codes
none is telling you that the function is not returning a value so you must use the return keyword
you should use return instead of print
def work_time(mathematic, chemistry, biology):
return mathematic*2 + chemistry*1 + biology*(0.5)
print("I will study " + str(work_time(1, 2,3) ) + " hours today")
I am working on procedural generation of character concepts. As part of this, I have set variables to change the pronouns in text so they will be correct for the generated character. However, for some reason, my code refuses to assign variables for this, resulting in output like "Bob is a Caucasian . works as..." It should read "Bob is a Caucasian male. He works as..."
Everywhere a pronoun or the word male or female should be, it prints nothing. Not to console, nor to file. When I strip the entire script down to just the code that calls for the function to run, and the function itself, it still wont assign the variables.
Other systems that use functions to assign variables are working 100% fine.
I've rewritten new functions 3 times to try different approaches to this. I've tried making the variables global. I don't know what else to try.
Here is the function.
def pronouns(sex1):
pronoun1, pronoun1alternate, pronoun2, pronoun2alternate = "", "", "", ""
if sex1 == "male":
pronoun1 = "He"
pronoun1alternate = "he"
pronoun2 = "His"
pronoun2alternate = "his"
elif sex1 == "female":
pronoun1 = "She"
pronoun1alternate = "she"
pronoun2 = "Her"
pronoun2alternate = "her"
return pronoun1, pronoun1alternate, pronoun2, pronoun2alternate
The variable sex1 is created much later on, and cannot be created within this function as I may want to expand this program for fantasy and sci-fi character concepts later on and decided to handle sex selection with each individual race in case I want to do something like Species-8472 in the future.
Here is how the function called later on.
p1, p1a, p2, p2a = pronouns(sex1)
I have been informed that python passes the values but not names. It didn't work when the function outputted p1 p1a and so on either. I was told using different names in the same order might fix the problem. It did not.
Here his how the variables are supposed to be used.
description = name + " is a " + race + " " + sex + ". " + p1 + " has " + eyes + " eyes. " + colorapp
It has no problem filling out the name, race, eye color, and the colorapp variable, but cannot ever, regardless of what I do, fill out sex, or any pronoun values.
No error messages occur when this code is run in isolation. It will print blank lines if asked to just print the pronouns or sex. However, sex1 will print successfully, as will almost every other variable in the program.
How are you assigning value to variable sex1? Make sure it is passed in lowercase. If this is the problem you can use sex1.lower() on lines 3 and 8 of the function.
How to print variable name by input, Example:
a = 1
b = 2
what_variable = input('Which Variable?: ') #User for example introduces 'b'
Console: 2
You can write
print(globals()[what_variable])
but it's not a good approach. Use a dict instead
You can use exec:
var = input('Which Variable?: ')
exec("print(" + var + ")")
Output:
Which Variable?: b
2
>>
Just do the following:
print(eval(input('Which Variable?: ')))
You can also do
print(globals()[input('Which Variable?: ')])
While the other answers seem to address the obvious solution, it's not very 'Pythonic'. The main issues with these is, by far, safety. Let's say that your user inputs apiKey, and you happen to have a variable by that name... let's just say your bank statement is probably looking at a slight increase in magnitude. What most people in these answers don't realise is that using .globals()[input()] is no safer than eval(input()), because, shockingly, people store private info in variables. Alternatively, if it points to a method, e.g
a = print
b = os.system
eval(input())()
I could enter any function name there, and the damage would be done before the second () executes.
Why? Well, let's take a look at how exec and eval work (I won't go into the difference here, see this question for that). All they do is evaluate the string as Python code, and (simplifying here) return the value of the evaluation:
var1 = 3
print(eval("var1"))
# ====is equal to====
var1 = 3
print(var1)
(where var1 as a string obviously comes from the input typed in)
But if someone enters something malicious, this is essentially the basis of an SQL injection:
(where userInput is substituted by a user's input into an input())
userInput = "a + os.system('reboot now')"
print(eval(userInput))
# ====is equal to====
print(a + os.system('shutdown now')
and you suddenly find your computer's off.
Therefore, we'd either use a:
Dictionary (or object): x={a:1, b:2}, then do x[input()]
Array x=[1, 2], then do x[["a", "b"].index(input())]
Simply don't. Find a way to work around it. What's wrong with an if/else set? It's not good practise, because of the safety concerns outlined above. What most people seem to miss about dictionaries (or my array option) is that if you enter a malformed input (i.e not a or b), it would result in either uncaught errors being thrown, or undefineds being thrown around. And if you're going to do input validation, you're using an if statement anyway, so why not do it from the onset?
So I'm working a quiz on Python as a project for an Intro to Programming course.
My quiz works as intended except in the case that the quiz variable is not being affected by the new values of the blank array. On the run_quiz function I want to make the quiz variable update itself by changing the blanks to the correct answer after the user has provided it.
Here's my code:
#Declaration of variables
blank = ["___1___", "___2___", "___3___", "___4___"]
answers = []
tries = 5
difficulty = ""
quiz = ""
#Level 1: Easy
quiz1 = "Python is intended to be a highly " + blank[0] + " language. It is designed to have an uncluttered " + blank[1] + " layout, often using English " + blank[2] + " where other languages use " + blank[3] + ".\n"
#Level 2: Medium
quiz2 = "Python interpreters are available for many " + blank[0] + " allowing Python code to run on a wide variety of systems. " + blank[1] + " the reference implementation of Python, is " + blank[2] + " software and has a community-based development model, as do nearly all of its variant implementations. " + blank[1] + " is managed by the non-profit " + blank[3] + ".\n"
#Level 3: Hard
quiz3 = "Python features a " + blank[0] + " system and automatic " + blank[1] + " and supports multiple " + blank[2] + " including object-oriented, imperative, functional programming, and " + blank[3] + " styles. It has a large and comprehensive standard library.\n"
#Answer and quiz assignment
def assign():
global difficulty
global quiz
x = 0
while x == 0:
user_input = raw_input("Select a difficulty, Press 1 for Easy, 2 for Medium or 3 for Hard.\n")
if user_input == "1":
answers.extend(["readable", "visual", "keywords", "punctuation"])
difficulty = "Easy"
quiz = quiz1
x = 1
elif user_input == "2":
answers.extend(["operating systems", "cpython", "open source", "python software foundation"])
difficulty = "Medium"
quiz = quiz2
x = 1
elif user_input == "3":
answers.extend(["dynamic type", "memory management", "programming paradigms", "procedural"])
difficulty = "Hard"
quiz = quiz3
x = 1
else:
print "Error: You must select 1, 2 or 3.\n"
x = 0
def run_quiz():
n = 0
global tries
global blank
print "Welcome to the Python Quiz! This quiz follows a fill in the blank structure. You will have 5 tries to replace the 4 blanks on the difficulty you select. Let's begin!\n"
assign()
print "You have slected " + difficulty + ".\n"
print "Read the paragraph carefully and prepare to provide your answers.\n"
while n < 4 and tries > 0:
print quiz
user_input = raw_input("What is your answer for " + blank[n] + "? Remember, you have " + str(tries) + " tries left.\n")
if user_input.lower() == answers[n]:
print "That is correct!\n"
blank[n] = answers[n]
n += 1
else:
print "That is the wrong answer. Try again!\n"
tries -= 1
if n == 4 or tries == 0:
if n == 4:
print "Congratulations! You are an expert on Python!"
else:
print "You have no more tries left! You can always come back and play again!"
run_quiz()
I know my code has many areas of improvement but this is my first Python project so I guess that's expected.
The problem is that your variable, quiz, is just a fixed string, and although it looks like it has something to do with blanks, it actually doesn't. What you want is 'string interpolation'. Python allows this with the .format method of str objects. This is really the crux of your question, and using string interpolation it's easy to do. I'd advise you to take some time to learn .format, it's an incredibly helpful function in almost any script.
I've also updated your code a bit not to use global variables, as this is generally bad practice and can lead to confusing, difficult to track bugs. It may also impair the uncluttered visual layout :). Here is your modified code, which should be working now:
quizzes = [
("""\
Python is intended to be a highly {} language.\
It is designed to have an uncluttered {} layout,\
often using English {} where other languages use {}
""", ["readable", "visual", "keywords", "punctuation"], "Easy"),
("""\
Python interpreters are available for many {}\
allowing Python code to run on a wide variety of systems.\
{} the reference implementation of Python, is {}\
software and has a community-based development model, as\
do nearly all of its variant implementations. {} is managed by the non-profit {}
""", ["operating systems", "cpython", "open source", "python software foundation"], "Medium"),
("""\
Python features a {} system and automatic {} and\
supports multiple {} including object-oriented,\
imperative, functional programming, and\
{} styles. It has a large and comprehensive standard library.
""", ["dynamic type", "memory management", "programming paradigms", "procedural"], "Hard")
]
#Answer and quiz assignment
def assign():
while True:
user_input = raw_input("Select a difficulty, Press 1 for Easy, 2 for Medium or 3 for Hard.\n")
if user_input == "1":
return quizzes[0]
elif user_input == "2":
return quizzes[1]
elif user_input == "3":
return quizzes[2]
else:
print "Error: You must select 1, 2 or 3.\n"
continue
break
def run_quiz():
n = 0
#Declaration of variables
blank = ["___1___", "___2___", "___3___", "___4___"]
tries = 5
print "Welcome to the Python Quiz! This quiz follows a fill in the blank structure. You will have 5 tries to replace the 4 blanks on the difficulty you select. Let's begin!\n"
quiz, answers, difficulty = assign()
print "You have selected {}.\n".format(difficulty)
print "Read the paragraph carefully and prepare to provide your answers.\n"
while n < 4 and tries > 0:
print quiz.format(*blank)
user_input = raw_input("What is your answer for {}? Remember, you have {} tries left.\n".format(blank[n], tries))
if user_input.lower() == answers[n]:
print "That is correct!\n"
blank[n] = answers[n]
n += 1
else:
print "That is the wrong answer. Try again!\n"
tries -= 1
if n == 4 or tries == 0:
if n == 4:
print "Congratulations! You are an expert on Python!"
else:
print "You have no more tries left! You can always come back and play again!"
run_quiz()
A little more on string interpolation:
You're doing a lot of "start of string " + str(var) + " end of string". This can be achieved quite simply with "start of string {} end of string".format(var)" - it even automatically does the str conversion. I've changed your quiz variables to have "{}" where either "__1__" etc should be displayed or the user's answer. You can then do quiz.format(*blank*) to print the 'most recent' version of the quiz. * here 'unpacks' the elements of blank into separate arguments for format.
If you find it more easy to learn with example usage, here are two usages of format in a simpler context:
>>> "the value of 2 + 3 is {}".format(2 + 3)
'the value of 2 + 3 is 5'
>>> a = 10
>>> "a is {}".format(a)
'a is 10'
I've also stored the information about each quiz in a list of tuples, and assign now has a return value, rather than causing side effects. Apart from that, your code is still pretty much intact. Your original logic hasn't changed at all.
Regarding your comment about objects:
Technically, yes, quizzes is an object. However, as Python is a 'pure object oriented language', everything in Python is an object. 2 is an object. "abc" is an object. [1, 2, 3] is an object. Even functions are objects. You may be thinking in terms of JavaScript - with all of the brackets and parentheses, it kind of resembles a JS Object. However, quizzes is nothing more than a list (of tuples). You might also be thinking of instances of custom classes, but it's not one of those either. Instances require you to define a class first, using class ....
A bit more on what quizzes actually is - it's a list of tuples of strings, lists of strings and strings. This is a kind of complicated type signature, but it's just a lot of nested container types really. It firstly means that each element of quizzes is a 'tuple'. A tuples is pretty similar to a list, except that it can't be changed in place. Really, you could almost always use a list instead of a tuple, but my rule of thumb is that a heterogenous collection (meaning stuff of different types) should generally be a tuple. Each tuple has the quiz text, the answers, and the difficulty. I've put it in an object like this as it means it can be accessed by indexing (using quiz[n]), rather than by a bunch of if statements which then refer to quiz1, quiz2, etc. Generally, if you find yourself naming more than about two variables which are semantically similar like this, it would be a good idea to put them in a list, so you can index, and iterate etc.
Only now have I read your question properly.
You first make your strings quiz1, quiz2 an quiz3.
You only do that once.
After that you change your blanks array.
But you don't reconstruct your strings.
So they still have the old values.
Note that a copy of elements of the blanks array is made into e.g. quiz1.
That copy doesn't change automagically after the fact.
If you want to program it like this, you'll have to rebuild your quiz1, quiz2 and quiz3 strings explicitly each time you change your blanks array.
General advice: Don't use so many globals. Use function parameters instead. But for a first attempt I guess it's OK.
[edit]
A simple modification would be:
Replace your quiz, quiz1, quiz2 and quiz3 by functions get_quiz (), get_quiz1 () etc. that get the most recent version, including the altered elements of blanks.
This modification doesn't make this an elegant program. But you'll come to that with a bit more experience.
A long shot in case you wonder (but don't try to bridge that gap in one step):
In the end Quiz will probably be a class with methods and attributes, of which you have instances.
To be sure: I think that experimenting like this will make you a good programmer, more than copying some ready to go code!