Python 如何循环我的整个程序?
我如何使我的代码返回到第五行,并要求玩家提供另一个角色?我需要使玩家能够输入他们想要的任意多的字符Python 如何循环我的整个程序?,python,Python,我如何使我的代码返回到第五行,并要求玩家提供另一个角色?我需要使玩家能够输入他们想要的任意多的字符 from __future__ import division #This imports the division module which allows the program to generate decimals from random import randint # This imports the random integer module which allows the
from __future__ import division #This imports the division module which allows the program to generate decimals
from random import randint # This imports the random integer module which allows the program to randomly generate integers
character = raw_input(str("Pick a character: martian,plutonian,human or dog\n>>")) # This asks the player what character they would like to play
strength = randint(3, 20) # This sets the range for the strength variable
speed = randint(3, 20) # This sets the range for the speed variable
height = randint(120, 183)/100 # This sets the range for the height variable (the variables are 100x the amount that the task states) it then divides the value by 100
# The reason as to why the height value is 100x what it is meant to be is because the height is a decimal and the randint function only works with real numbers
heart_rate = randint(60, 110) # This sets the range for the heart rate variable
if character=="martian": # This makes the following only occur if the player inputs "martian"
strength += 3 #This adds 3 to the value of strength
speed +=2 #This adds 2 to the value of speed
if character =="plutonian": #This makes the following only occur if the player inputs "plutonian"
heart_rate+=7 #This adds 7 to the value of heart rate
height+=1 #This adds 1 to the value of height
if character=="human": #This makes the following only occur if the player inputs "human"
heart_rate+=3 #This adds 3 to the heart rate value
if character=="dog": #This makes the following only occur if the player inputs dog
height-=3 #This takes 3 away from the height value
print "Your strength is", strength # This prints the strength of the character
print "Your speed is", speed # This prints the speed of the character
print "Your heart rate is", heart_rate # This prints the heart rate of the character
print "Your height is", height # This prints the height of the character
file_txt=open('Character Statistics.txt', 'a',) #This creates and names the text file
file_txt.write(str("Character Statistics:\n")) #This makes a title within the text document
file_txt.write("Your strength is: ") #This prints text into the text file
file_txt.write(str(strength)+"\n") #This adds the strength value to the txt file
file_txt.write("Your speed is: ") #This prints text into the text file
file_txt.write(str(speed)+"\n") #This adds the speed value to the txt file
file_txt.write("Your heart rate is: ") #This prints text into the text file
file_txt.write(str(heart_rate)+"\n") #This adds the heart rate value to the txt file
file_txt.write("Your height is is: ") #This prints text into the text file
file_txt.write(str(height)+"\n") #This adds the height value to the txt file
使用
while
循环:
character_list = ['martian', 'plutonian', 'human', 'dog']
while True:
character = raw_input("Pick a character: %s\n>>" % ', '.join(character_list))
if character not in character_list:
# The user is done
break
如果用户输入了无效字符,则假定它们已完成并中断循环。将其余的字符生成代码放在
if
节之后。围绕要循环的逻辑进行循环。不要把它复杂化,把整个逻辑放在循环中。我也建议把它贴在网上。此代码段中有很多需要改进的地方创建一个函数(或者更好的是创建一个字符类)。看不到代码中的“第五行”或任何用户输入,但无论该行在哪里,某种形式的while
或for
语句将完成一个循环。检查ceejeyoz已经说过的话;你几乎已经自己解决了这个问题。“循环”是您所需要的,因此,找出如何在python中实现它,编写它,您就完成了。谢谢您的回答。