I'm currently making a text game, but part of my game won't work, and I can't figure out why. This is the coding:
c1pp1 = ["Joe", "Frank", "Annie"]
while cp_ptt < 3:
s= input()
#FIX THIS!!
c1pp = input(n/ + ": Who should I talk to first?\nYour options are:\n" +\
c1pp1[0] + ", an ex-policeman\n" + c1pp1[1] + ", a carpenter\n" +\
c1pp1[2] + ", an architect\n")
The outcome I get is:
Traceback (most recent call last):
File "N:\MyWork\Year 9\Other\Random\Making a text game out of song lyrics\No.1;
Phantom Thief F's Scenario ~Mystery of the Missing Diamond~.py", line 103,
in <module>
c1pp = input(n/ + ": Who should I talk to first?\nYour options are:\n" + c1pp1[0] + ", an ex-policeman\n" + c1pp1[1] + ", a carpenter\n" + c1pp1[2] + ", an architect\n")
TypeError: bad operand type for unary +: 'str'
Can someone please tell me what is wrong with my code?
Some parentheses to highlight the expression will help.
Your code is trying to parse n / (+ ": Who should I talk to first?\nYour options are:\n") but + "any string" is not a valid use of the unary + operator.
Like others have pointed out in the comments, you've probably made a typo. Without the rest of your code, I can't tell you what you need to do to get your desired output, but you can probably start by removing that /.
Related
I am trying to make a randomly generated fantasy story, and it is not letting me refer to a list inside a list.
line 36 is the bottom line of the code shown.
I've looked it up and did what it said, but it isn't working! maybe because it was for printing text and a list, not making a list with a list inside it, and I don't understand the error message...
itemA = ["letter ", "scroll ", "message "]
specialPlace = ["waterfall.'", "rock pile.'"]
events = [f"a {mysticalCreature[randint(0,8)]} comes up to you and gives you a {itemA[randint(0,2)]} saying, 'come to the " + specialPlace + ", so you thank them and walk away."]```
Traceback (most recent call last):
File "main.py", line 36, in <module>
events = [f"a {mysticalCreature[randint(0,8)]} comes up to you and gives you a {itemA[randint(0,2)]} saying, 'come to the " + specialPlace + ", so you thank them and walk away."]
TypeError: can only concatenate str (not "list") to str
specialMessage is a list, and you're trying to use + to concatenate it with strings. If you want to choose a random value from it to put there, replace it with random.choice(specialMessage). Otherwise, you need to determine which string in that list you want to use in some other way.
You should probably be using random.choice in the other cases too; as written, if you change the length of any list you have to manually adjust the associated randint calls, while random.choice(mysticalCreature) gets the same result without hard-coding in any magic numbers.
import random
mysticalCreature = ["Fairy ", "Unicorn ", "Vampire (a fairy one) ", "Sasquatch ", "Dragon ", "Pheonix ", "Griffin ", "Satyr ", "Centaur " ]
itemA = ["letter ", "scroll ", "message "]
specialPlace = ["waterfall.'", "rock pile.'"]
events = [f"a {mysticalCreature[random.randint(0,8)]} comes up to you and gives you a {itemA[random.randint(0,2)]} saying, 'come to the " + specialPlace[random.randint(0,1)] + ", so you thank them and walk away."]
print(events)
I have a very simple problem that I have been unable to find the solution to, so I thought I'd try my "luck" here.
I have a string that is created using variables and static text altogether. It is as follows:
filename_gps = 'id' + str(trip_id) + '_gps_did' + did + '_start' + str(trip_start) + '_end' + str(trip_end) + '.json'
However my problem is that pylint is complaining about this string reprensentation as it is too long. And here is the problem. How would I format this string representation over multiple lines without it looking weird and still stay within the "rules" of pylint?
At one point I ended up having it looking like this, however that is incredible "ugly" to look at:
filename_gps = 'id' + str(
trip_id) + '_gps_did' + did + '_start' + str(
trip_start) + '_end' + str(
trip_end) + '.json'
I found that it would follow the "rules" of pylint if I formatted it like this:
filename_gps = 'id' + str(
trip_id) + '_gps_did' + did + '_start' + str(
trip_start) + '_end' + str(
trip_end) + '.json'
Which is much "prettier" to look at, but in case I didn't have the "str()" casts, how would I go about creating such a string?
I doubt that there is a difference between pylint for Python 2.x and 3.x, but if there is I am using Python 3.x.
Don't use so many str() calls. Use string formatting:
filename_gps = 'id{}_gps_did{}_start{}_end{}.json'.format(
trip_id, did, trip_start, trip_end)
If you do have a long expression with a lot of parts, you can create a longer logical line by using (...) parentheses:
filename_gps = (
'id' + str(trip_id) + '_gps_did' + did + '_start' +
str(trip_start) + '_end' + str(trip_end) + '.json')
This would work for breaking up a string you are using as a template in a formatting operation, too:
foo_bar = (
'This is a very long string with some {} formatting placeholders '
'that is broken across multiple logical lines. Note that there are '
'no "+" operators used, because Python auto-joins consecutive string '
'literals.'.format(spam))
I am making an average calculator in python, i have to save the averages in a text file and have done it like this:
Mean = statistics.mean(aver)
Mode = statistics.mode(aver)
Medi = statistics.median(aver)
file = open("Averages.txt", "a")
file.write("\n\nYour numbers are" + aver +
"\nMean : " + Mean +
"\nMode : " + Mode +
"\nMedian : " + Medi)
(aver is a list of numbers i am finding the average of)
when i try to run this part of the code, i recieve the error message:
TypeError: Can't convert 'list' object to str implicitly
I tried basic stuff like adding 'str' on but it doesnt help.
file.write("\n\nYour numbers are" + **aver** +
this would be better as something like this:
aver = " " + ", ".join(aver) + " "
which converts your list to a comma separated string.
im writing some code to print a triangle with so many rows but when i try it it says,
how many rows in the triangle 5
Traceback (most recent call last):
File "U:\School\Homework\year 8\module 3\IT\python\lesson 10\extention task set by Mr Huckitns.py", line 6, in <module>
triangle(5)
File "U:\School\Homework\year 8\module 3\IT\python\lesson 10\extention task set by Mr Huckitns.py", line 5, in triangle
print((x*(str(" ")))(int(i)*(str("*")))((int(row)-int(i))*(str(" "))))
TypeError: 'str' object is not callable
anybodyknow whats going on here
the code i am using is
inttrow=int(input("how many rows in the triangle "))
def triangle(row):
for i in range(1,row):
x=int(inttrow)-int(i)
print((x*(str(" ")))(int(i)*(str("*")))((int(row)-int(i))*(str(" "))))
triangle(5)
The problem is the punctuation in your print statement. You're printing three strings in succession, but you forgot to put any concatenation operation between them. Try this:
print ((x*(str(" "))) + (int(i)*(str("*"))) + ((int(row)-int(i))*(str(" "))))
Further, why are you doing all these type coercions -- all of those variables already have the proper types. Cut it down to this:
print (x*" " + i*"*" + (row-i)*" ")
You are trying to contatenate strings by placing them near each other in the code like this:
("hello")(" ")("world")
Try that on the command line and see what happens. It is not the syntax of the language you are using. Then try using the plus sign.
"hello" + " " + "world"
So I am defining a function for use in a ArcGIS tool that will verify attributes, catch errors, and obtain user input to rectify those error. I want the tool to select and zoom to the segment that is being currently assessed so that they can make an informed decision. This is what I have been using, and it works well. But the CONVWGID is the variable that will be changing, and I'm not sure how to input that variable into an SQL statement without causing errors.
This is how I had tested the logic:
def selectzoom():
arcpy.SelectLayerByAttribute_management(Convwks, "NEW_SELECTION", " [CONVWGID] = 10000001")
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
Then I needed to work the variable into the function in order to accept different CONVWGID values, which gives me a Runtime/TypeError that I should have known would happen.
Runtime error -
Traceback (most recent call last): - File "string", line 1, in module - TypeError: cannot concatenate 'str' and 'int' objects
def selectzoom(convwkgid):
delimfield = '" [CONVWGID] = ' + convwkgid + ' "'
arcpy.SelectLayerByAttribute_management(Convwks, "NEW_SELECTION", delimfield)
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
And when I alter the delimfield line to change the integer into a string, it selects all of the attributes in the entire feature class. Not just the one that had been passed via the function call.
delimfield = '"[CONVWGID] = ' + str(convwkgid) + '"'
I'm not amazing with SQL and maybe I'm missing something basic with this statement, but I can't figure out why it won't work when I'm basically giving it the same information:
"[CONVWGID] = 10000001"
'"[CONVWGID] = ' + str(convwkgid) + '"'
It turned out to be the extra inclusion of Double quotes inside of my single quotes that raised this problem.
Thanks to #Emil Brundage for the help!
Let's say convwkgid = 10000001
'"[CONVWGID] = ' + str(convwkgid) + '"' doesn't equal "[CONVWGID] = 10000001"
'"[CONVWGID] = ' + str(convwkgid) + '"' would actually be '"CONVWGID] = 10000001"'
Try instead:
'[CONVWGID] = ' + str(convwkgid)