This is my code (of which IDLE highlights 'print' and says 'Invalid Syntax':
import random
initial_val = 10
attributes = str("Character 1's Strength: ",(random.randint(1,12)/random.randint(1,4) + initial_val), \
"\nCharacter 1's Skill: ",(random.randint(1,12)/random.randint(1,4) + initial_val), \
"\n\nCharacter 2's Strength: ",(random.randint(1,12)/random.randint(1,4) + initial_val), \
"\nCharacter 2's Skill: ",(random.randint(1,12)/random.randint(1,4) + initial_val)
print(attributes)
file = open("Attribute.txt", "w")
file.write(attributes)
file.close()
input("\n\nPress enter to exit")
Why does it do this? It's probably extremely obvious, but I'm new to programming. Many Thanks
The preceding line is missing a closing )
parenthesis:
attributes = str("Character 1's Strength: ",(random.randint(1,12)/random.randint(1,4) + initial_val), \
"\nCharacter 1's Skill: ",(random.randint(1,12)/random.randint(1,4) + initial_val), \
"\n\nCharacter 2's Strength: ",(random.randint(1,12)/random.randint(1,4) + initial_val), \
"\nCharacter 2's Skill: ",(random.randint(1,12)/random.randint(1,4) + initial_val)
The str(
function call was never closed.
Use string formatting and """
triple-quoted string instead for a far more readable declaration:
attributes = """\
Character 1's Strength: {}
Character 1's Skill: {}
Character 2's Strength: {}
Character 2's Skill: {}
""".format(
random.randint(1, 12) / random.randint(1, 4) + initial_val,
random.randint(1, 12) / random.randint(1, 4) + initial_val,
random.randint(1, 12) / random.randint(1, 4) + initial_val,
random.randint(1, 12) / random.randint(1, 4) + initial_val)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.