4

I am a curious little bit, why the break statement is not working in ternary operator in Python? Ternary operator here I am referring to this syntax [on_true] if [expression] else [on_false]

Below is my code

import random

def rolldice():
    print(random.randint(1, 6))

while True:
    ip = input("Roll dice ? y/n ")

    # Below line will gives you SyntaxError: invalid syntax
    # rolldice() if ip == 'y' or ip =='Y' else break

    if ip == 'y' or ip =='Y':
        rolldice()
    else:
        break

The commented line rolldice() if ip == 'y' or ip =='Y' else break is not working while another code syntax which is logically similar to this one is working fine.

Abhijit Kumbhar
  • 923
  • 3
  • 23
  • 49

0 Answers0