Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python If语句未检测到字符串或不';不要采取行动_Python_Python 3.6 - Fatal编程技术网

Python If语句未检测到字符串或不';不要采取行动

Python If语句未检测到字符串或不';不要采取行动,python,python-3.6,Python,Python 3.6,我想做一个机器人,和玩家玩石头、布、剪刀。 但每次我试着运行脚本,并输入Stein(德语表示摇滚乐), if语句没有检测到它或没有采取任何操作 以下是我目前的代码: import random import time print("Anfangs Buchstaben immer groß schreiben!") time.sleep(2) outcomes2 = ("Stein") Runde = 0 while True: Player = input("Deine Wahl

我想做一个机器人,和玩家玩石头、布、剪刀。 但每次我试着运行脚本,并输入
Stein
(德语表示摇滚乐), if语句没有检测到它或没有采取任何操作

以下是我目前的代码:

import random
import time

print("Anfangs Buchstaben immer groß schreiben!")

time.sleep(2)

outcomes2 = ("Stein")

Runde = 0

while True:

 Player = input("Deine Wahl: ")

 for i in range(1):
    outcomes = print(random.choice(outcomes2))

 if outcomes == Player:
    print("draw")
    Runde + 1
    continue
问题1 您正在对字符串进行迭代,并随机选择一个字符

我假设你想要:

>>> outcomes2 = ("Stein", )
>>> print(random.choice(outcomes2))
Stein
现在,通过指定
,可以迭代字符串的元组(大小为1的元组)。除非添加更多字符串,否则最终只能得到“Stein”,如

outcomes2 = ("Stein", "Name2", "Name3", ...)
问题2 您需要
结果=随机。选择(结果2)
。不要将该值赋给
print
语句,因为
print
不返回任何值

把它放在一起

outcomes2 = ("Stein", )

Runde = 0
while True:
    Player = input("Deine Wahl: ")
    outcomes = random.choice(outcomes2)

    if outcomes == Player:
       print("draw")
       Runde + 1
       continue

对于范围(1)中的i:
-为什么?你甚至不用
i
!还请注意,
outcouts=print(…)
表示
结果为None
;print函数不返回任何内容。解释为什么逗号会产生差异(或引用有差异的内容)会很有帮助。@jornsharpe我确实提到过他将迭代一个元组(因此,
是指定元组的语法)。但你是对的。对于初学者来说,这没什么可谈的。编辑。非常感谢,主要的一期是第二期,我也刚刚意识到我在重复一个问题string@Forex在第2期之后,主要问题将变成第1期!干杯,很高兴为您提供帮助。虽然此代码可以回答问题,但提供有关如何和/或为什么解决问题的其他上下文将提高答案的长期价值。此外,您可能还应该格式化它。
outcomes2 = ("Stein", )

Runde = 0
while True:
    Player = input("Deine Wahl: ")
    outcomes = random.choice(outcomes2)

    if outcomes == Player:
       print("draw")
       Runde + 1
       continue
`import time
import random
comp_score=0
play_score=0
outcome=('rock','paper','scissor')

while True:     
    player = raw_input("Enter any?");   
    out=random.choice(outcome) 
    print "COMP->"+out      
    if out==player:     
        print "draw"    
    elif out=="rock"and player=="scissor":      
    comp_score+=1;  
    elif out=="scissor" and player=="rock":     
        play_score+=1;  
    elif out=="rock" and player=="paper":   
        play_score+=1;  
    elif out=='paper' and player=='rock':   
        comp_score+=1;  
    elif out=="scissor" and player=="paper": 
            play_score+=1;  
    elif out=='paper' and player=="scissor":
            comp_score+=1;  
    elif player=="quit":        
    break; 
    print "GAME over"
    print "PLayer score: ",play_score
    print "Comp score ",comp_score`