Search code examples
schemeracket

Racket: Conditional Statements Not Matching String Equality in Gambling Simulation"


Problem Description:

I'm working on a simple gambling simulation in Racket, but I'm encountering an issue where the conditional statements are not correctly matching string equality. The goal is to output "W" when the input bet matches the randomly chosen color c. However, the program seems to always output "L" even when the input matches c.

Code:

(define (ro)
  (define bet (string-trim (read-line)))
  (define n (+ (random 36) 1))
  (define c (list-ref '(red black) (random 2)))
  
  (display (string-append bet " " c " "))
  
  (cond
    [(equal? bet c) (display "W")]
    ; Uncomment the next line if further conditions are relevant
    ; [(equal? bet "1-18") (if (<= n 18) (display "W") (void))]
    ; Add more conditions as needed
    [else (display "L")]
  ))

(ro)

Expected Behavior:

I expect the program to output "W" when bet matches the color c. Additionally, if there are further conditions, uncomment the corresponding line and adapt the conditions accordingly.

Actual Behavior:

The program consistently outputs "L" even when bet matches c. I've tried different input values, but the issue persists.

Additional Notes:

I suspect there might be an issue with string comparison or the way I'm handling input. Any insights into what might be causing this problem and how to resolve it would be greatly appreciated.

Thank you!


Solution

  • c is a symbol, bet is a string. They will never be equal, even if they contain the same text.

    Change c to a string.

    (define c (list-ref '("red" "black") (random 2)))
    

    Or you can convert the symbol to a string after selecting it:

    (define c (symbol->string (list-ref '(red black) (random 2))))