Reputation: 29
I have developed a piece of code for school and i have got to the end a run into a final problem, to finish the game i need to print off the end values of the results of the sums carried out and i am having problem including the variable names as inputted by the user, here is the code;
import random
print('Welcome to the game')
char1=input('What is the fist characters name: ')
char2=input('What is the second characters name: ')
char1st=int(input('What is the strength of '+char1))
char1sk=int(input('What is the skill of '+char1))
char2st=int(input('What is the strength of '+char2))
char2sk=int(input('What is the skill of '+char2))
strmod = abs (char2st - char1st) // 5
sklmod = abs (char2sk - char1sk) //5
char1rl=random.randint(1,6)
char2rl=random.randint(1,6)
if char1rl>char2rl:
char1nst=(char1st+strmod)
char1nsk=(char1sk+sklmod)
char2nsk=(char2sk-sklmod)
char2nst=(char2st-strmod)
elif char2rl>char1rl:
char2nst=(char2st+strmod)
char2nsk=(char2sk+sklmod)
char1nsk=(char1sk-sklmod)
char1nst=(char1st-strmod)
else:
print('both rolls where the same, no damage was given or taken')
if char1nst <= 0:
print(str(+char1+' has died'))
elif char2nst <=0:
print(str(+char2+' has died'))
else:
print(+char1( ' now has a strength value of '+char1nst' and a skill value of '+char1nsk'.'))
print(+char2( ' now has a strenght value of '+char2nst' and a skill value of '+char2nsk'.'))
I wrote the bit at the end in the hope that it would print the end values but i get a syntax error ?! and don't have clue why it is happening. Can someone please help me edit the last four lines so it will print in the format of:
Bob now has a strength value of 4 and a skill value of 7
I have used my method before but its not working this time so if someone could point out where i went wrong and how to amend this problem that would be great !!!!
Upvotes: 0
Views: 61
Reputation: 67
well since your using python3 why not use .format? insert {} into your string where you want a value, then call the format() method on that string with the values you want in the correct order.
else:
temp = "{} now has a strength value of {} and a skill value of {}."
print(temp.format(char1, char1nst, char1nsk))
print(temp.format(char2, char2nst, char2nsk))
cleanest soluton if you ask me.
Upvotes: 0
Reputation: 1
else: print(char1 +' now has a strength value of '+char1nst+' and a skill value of '+char1nsk+'.') print(char2 +' now has a strenght value of '+char2nst+' and a skill value of '+char2nsk+'.')
Upvotes: 0
Reputation: 11391
You have concatenation operators (+
) in places where you don't need them (at the beginning of print()
and you don't have them in places where you do need them (after the variable in of '+char1nsk'.'
). That's what's causing the syntax error.
You might consider string formatting instead of string concatenation:
print "%s now has a strength value of %d and a skill value of %d" % (char1, char1nst, char1nsk)
Upvotes: 2
Reputation: 1123350
You are trying to use the +
operator without anything to append:
print(str(+char2+' has died'))
You don't need the str
nor the +
operators there, just use multiple arguments to the print()
function:
if char1nst <= 0:
print(char1, 'has died'))
elif char2nst <=0:
print(char2, 'has died'))
else:
print(char1, 'now has a strength value of', char1nst, 'and a skill value of', str(char1nsk) + '.'))
print(char2, 'now has a strength value of', char2nst, 'and a skill value of', str(char2nsk) + '.'))
Only in the last two lines do I use str()
and +
to avoid a space between the value an the .
full stop.
You could, instead, use string formatting with the str.format()
method to get a more readable string formatting option for those last 2 lines:
template = '{} now has a strength value of {} and a skill value of {}.'
print(template.format(char1, char1nst, char1nsk))
print(template.format(char2, char2nst, char2nsk))
Because the text is the same for both lines, you can re-use a template string here.
Upvotes: 2
Reputation: 383
Try removing the + int the beginning of the print statements. Change it to this:
print char1 +' has died'
print char2 +' has died'
Upvotes: 0