Concatenating strings then printing - python

print ("Tag Value " + i.tags.get('Name'))
gives me:
File "./boto_test.py", line 19, in main
print ("Tag Value" + i.tags.get('Name'))
TypeError: cannot concatenate 'str' and 'NoneType' objects
What is the correct way to do this?

Or just convert whatever you get from i.tags to string:
print ("Tag Value " + str(i.tags.get('Name')))

i.tags doesn’t contain a 'Name' key. What should the result be for None? Just pass it as a second argument to get:
print ("Tag Value " + i.tags.get('Name', 'None'))

Try converting it to the string data type by str() function.
Use the following code for the same line:
print ("Tag Value" + str(i.tags.get('Name')))

Related

I want to have a word print alongside a textio that describes if the file is readable

The error it gives me is:
expected type 'str', got 'bool' instead"
code:
stuff = open("stuff.txt", "r")
print("File readable = " + stuff.readable())
print(stuff.readlines())
stuff.close()
You need to convert boolean to string:
print("File readable = " + str(stuff.readable()))
Or use f-strings or formatted string literals:
print(f"File readable = {stuff.readable()}")

argument of type 'int' is not iterable Issue

What I trying to do is picking random integers from a value, for example: 1:32 will be an input, I will split by the : and then select a random value. Then Selenium will select the dropdown based on what value is returned.
My code:
# SELECT
if register_parts[3] == "SELECT":
if register_parts[0] + '="' + register_parts[1] + '"' in self.driver.page_source:
_select_value = ""
if ":" in register_parts[2]:
_select_value = self.get_random_value_between(register_parts[2])
_select = Select(selenium_action)
_select.select_by_visible_text(_select_value)
self.write_to_debug_file("self.select_by_visible_text(" + _select_value + ") --> SELECT --> [ " + _select_value + " ]")
else:
_select_value = register_parts[2]
_select = Select(selenium_action)
_select.select_by_visible_text(_select_value)
self.write_to_debug_file("self.select_by_visible_text(" + _select_value + ") --> SELECT --> [ " + _select_value + " ]")
Additional function:
def get_random_value_between(self, input_values):
''' this function will return a random value between: x:x or 1:31 for example ... '''
parts = input_values.split(':')
return random.randrange(int(parts[0]), int(parts[1]))
The problem is on this line:
_select.select_by_visible_text(_select_value)
I'm getting the error:
argument of type 'int' is not iterable
From reading up, I think the issue lies in the fact I am doing:
if ":" in
I could be wrong. I'm not sure how to fix it. Any help on the issue would be appreciated. As far as I can see, the code should work, but I must be missing something. I have read a few threads on here regarding the error but it's still not sinking totally in.
If possible, cast _select_value as string before using _select.select_by_visible_text.
And recast as int values after the iteration.
I think this is correct. If the error is on if and not the else, then your passing an Int as an argument to a method that needs a text/str value.
Just try the following line:
_select.select_by_visible_text(str(_select_value))

Hangman code having trouble accessing other file

So I made a program for hangman that accesses an input file but it is having trouble accessing it once I type it in.
This is the function that is calling the file
def getWord(filename):
print("Loading from file...")
inputFile = open(filename, 'r')
wordlist = inputFile.read().splitlines()
print(len(wordlist) + " lines loaded.")
return wordlist
filename = input("What file will the words come from? ")
wordlist = getWord(filename)
theWordLine = random.choice(wordlist)
game(theWordLine)
And this is the file itself
person,Roger
place,Home
phrase,A Piece Of Cake
The error it is giving me is this
File "hangman.py' , line 77, in <module>
wordlist = getWord(filename)
File "hangman.py' , line 10, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Can anyone help me out?
The error states: TypeError: unsupported operand type(s) for +: 'int' and 'str'. That means that you cannot use + with something of type int and something of type str. the len function returns an int. Thus you need to cast it into a str before you being able to concatenate it with another str
It should be print(str(len(wordlist)) + " lines loaded.") instead of print(len(wordlist) + " lines loaded.")
You may also want to use string formatting as a comment mentions. If you are using python 3.6 or higher, you can try f-strings: f'{len(wordlist)} lines loaded}'.
It has nothing to do with reading the file. Read the error: an integer and string cannot be added.
Why are you getting this error? Because len() returns an integer, not a string. You can cast len()'s return to a string, or you can just use an f-string:
f'{len(wordlist)} lines loaded}'
Use print(len(wordlist), " lines loaded.") instead of print(len(wordlist) + " lines loaded.")
print(len(wordlist) + " lines loaded.") is causing your issue as it is trying to apply the + operand to variables of different datatypes.
You could use print("{} lines loaded".format(len(wordlist))) to avoid this.

'/' understood as a float?

I am looping through a pandas dataframe and trying to append the result on a conditional statement. However, my code seems to be posing a problem stopping me from append what I want although it prints fine but displays the error in the end. here is my code below:
counta=[]
for line in ipcm_perf['Alarms']:
if '/' in line:
print (line)
the error I get is the following :
2 for line in ipcm_perf['Alarms']:
----> 3 if ('/') in line:
4 print (line)
5
TypeError: argument of type 'float' is not iterable
I really do not know why Python is flagging that line. where's the float? Everything is being printed but with error at the bottom. It is stopping from appending.
your problem is that you are trying to check if there is a string (/) in a floating number (line), and Python does not like that.
That's because when you write "/" in some_string Python iterates through each char of some_string, but he can iterate through a floating number.
you can double check this by simply running:
if '/' in 3.14:
print("something")
output:
TypeError: argument of type 'float' is not iterable
I suppose that you're searching for a / because you've seen that somewhere in the column. If that's the case, it probably is in a string, and if that's the case, a quick&dirty way to clean your data can be:
if type(line) == str:
if "/" in line:
print(line)
else:
print("I am not a string")
and with line = 3.14, it returns:
I am not a string
and with line = "4/2", it returns:
I am a string

TypeError: Can't convert 'builtin_function_or_method' object to str implicitly

I'm making a simple mad libs program in python 3 where the user enters in nouns and pronouns and the program is supposed to print out the inputs from the user.
Here is my code:
print ("Welcome to Mad Libs. Please enter a word to fit in the empty space.")
proper_noun = input("One day _________ (Proper Noun)").lower()
ing_verb = input("Was __________ (Verb + ing) to the").lower()
noun1= input("to the _________ (Noun)").lower()
pronoun1 = input("On the way, _____________ (Pronoun)").lower()
noun2 = input("Saw a ________ (Noun).").lower
pronoun2 = input("This was a surprise so ________ (Pronoun)").lower()
verb2 = input("_________ (verb) quickly.").lower()
#Asks user to complete the mad libs
print ("One day " + proper_noun)
print ("Was " + ing_verb + " to the")
print (noun1 + ". " + "On the way,")
print (pronoun1 + " saw a " + noun2 + ".")
print ("This was a surprise")
print ("So " + pronoun2 + " " + verb2 + " quickly.")
Getting this error code: TypeError: Can't convert 'builtin_function_or_method' object to str implicitly
On this line:
print (pronoun1 + " saw a " + noun2 + ".")
Fairly new to python, so i'm not entirely sure what this error means and how to fix it, can somebody explain this error code to me please?
The issue is with the noun2 variable
noun2 = input("Saw a ________ (Noun).").lower
You are assigning the function .lower to it , not the result of calling it . You should call the function as .lower() -
noun2 = input("Saw a ________ (Noun).").lower()
For Future readers
when you get an issues such as - TypeError: Can't convert 'builtin_function_or_method' object to str implicitly - when trying to concatenate variables contain strings using + operator.
The issue basically is that one of the variables is actually a function/method, instead of actual string.
This (as in the case with OP) normally happens when you try to call some function on the string, but miss out the () syntax from it (as happened in the case of OP) -
name = name.lower #It should have been `name.lower()`
Without the () syntax, you would be simply assigning the function/method to the variable , and not the actual result of calling the function. To debug such issues you can check out the lines where you are assigning to the variables , used in the line throwing the error, and check if you missed out on calling any function.

Categories

Resources